我在Jupyter Notebook中提供了一个数据帧。数据帧的初始数据类型是float。我想将印刷表的行1 & 3作为整数呈现,并将行2 & 4呈现为百分比。我怎么做? (我花了大量的时间寻找,但没有成功的解决方案)格式化Jupyter Notebook数据帧输出中的特定行
下面是我使用的代码:
#Creating the table
clms = sales.columns
indx = ['# of Poeple','% of Poeple','# Purchased per Activity','% Purchased per Activity']
basic_stats = pd.DataFrame(data=np.nan,index=indx,columns=clms)
basic_stats.head()
#Calculating the # of people who took part in each activity
for clm in sales.columns:
basic_stats.iloc[0][clm] = int(round(sales[sales[clm]>0][clm].count(),0))
#Calculating the % of people who took part in each activity from the total email list
for clm in sales.columns:
basic_stats.iloc[1][clm] = round((basic_stats.iloc[0][clm]/sales['Sales'].count())*100,2)
#Calculating the # of people who took part in each activity AND that bought the product
for clm in sales.columns:
basic_stats.iloc[2][clm] = int(round(sales[(sales[clm] >0) & (sales['Sales']>0)][clm].count()))
#Calculating the % of people who took part in each activity AND that bought the product
for clm in sales.columns:
basic_stats.iloc[3][clm] = round((basic_stats.iloc[2][clm]/basic_stats.iloc[0][clm])*100,2)
#Present the table
basic_stats
这里的印刷表: Output table of 'basic_stats' data frame in Jupyter Notebook
的可能的复制[如何显示使用列的格式字符串彩车的熊猫数据框?](http://stackoverflow.com/questions/20937538/how-to-display-pandas-dataframe-of-浮动使用格式字符串为列) – IanS
我会建议转置您的表,将解决方案应用于建议的副本,然后转置回来显示它。 – IanS