Python 3.x 数据透视表百分比计算
给定以下数据框和数据透视表:Python 3.x 数据透视表百分比计算,python-3.x,pandas,pivot-table,percentage,Python 3.x,Pandas,Pivot Table,Percentage,给定以下数据框和数据透视表: import pandas as pd df=pd.DataFrame({'A':['x','y','z','x','y','z'], 'B':['one','one','one','two','two','two'], 'C':[2,18,2,8,2,18]}) df A B C 0 x one 2 1 y one 18 2 z o
import pandas as pd
df=pd.DataFrame({'A':['x','y','z','x','y','z'],
'B':['one','one','one','two','two','two'],
'C':[2,18,2,8,2,18]})
df
A B C
0 x one 2
1 y one 18
2 z one 2
3 x two 8
4 y two 2
5 z two 18
table = pd.pivot_table(df, index=['A', 'B'],aggfunc=np.sum)
C
A B
x one 2
two 8
y one 18
two 2
z one 2
two 18
我想在这个数据透视表中添加两列;一个显示所有值的百分比,另一个显示A列中的百分比,如下所示:
C % of Total % of B
A B
x one 2 4% 10%
two 18 36% 90%
y one 2 4% 20%
two 8 16% 80%
z one 2 4% 10%
two 18 36% 90%
额外学分:
table['% of Total'] = (table.C / table.C.sum() * 100)
table['% of B'] = (table.C / table.groupby(level=0).C.transform(sum) * 100)
table.loc['total', :] = table.sum().values
print table
C % of Total % of B
A B
x one 2.0 4.0 20.0
two 8.0 16.0 80.0
y one 18.0 36.0 90.0
two 2.0 4.0 10.0
z one 2.0 4.0 10.0
two 18.0 36.0 90.0
total 50.0 100.0 300.0
我想要一个底部的汇总行,该行包含C列的总和(如果它在接下来的两列中也包含100%,则可以,但这些列不需要任何内容)。您可以使用:
table['% of Total'] = (table.C / table.C.sum() * 100).astype(str) + '%'
table['% of B'] = (table.C / table.groupby(level=0).C.transform(sum) * 100).astype(str) + '%'
print table
C % of Total % of B
A B
x one 2 4.0% 20.0%
two 8 16.0% 80.0%
y one 18 36.0% 90.0%
two 2 4.0% 10.0%
z one 2 4.0% 10.0%
two 18 36.0% 90.0%
但对于真实数据,我认为不推荐使用int
,最好是使用
额外学分:
table['% of Total'] = (table.C / table.C.sum() * 100)
table['% of B'] = (table.C / table.groupby(level=0).C.transform(sum) * 100)
table.loc['total', :] = table.sum().values
print table
C % of Total % of B
A B
x one 2.0 4.0 20.0
two 8.0 16.0 80.0
y one 18.0 36.0 90.0
two 2.0 4.0 10.0
z one 2.0 4.0 10.0
two 18.0 36.0 90.0
total 50.0 100.0 300.0
我想你们是我的熊猫守护天使。上帝保佑你们所有人!