DataFrame分组依据上的 pandas 百分比计数 [英] Pandas Percentage count on a DataFrame groupby
本文介绍了DataFrame分组依据上的 pandas 百分比计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个DataFrame(mydf
),如下所示:
I have a DataFrame (mydf
) along the lines of the following:
Index Feature ID Stuff1 Stuff2
1 True 1 23 12
2 True 1 54 12
3 False 0 45 67
4 True 0 38 29
5 False 1 32 24
6 False 1 59 39
7 True 0 37 32
8 False 0 76 65
9 False 1 32 12
10 True 0 23 15
..n True 1 21 99
我正在尝试为每个ID
(0或1)计算Feature
的正确和错误百分比,并且我正在为每个ID寻找两个输出:
I am trying to calculate the True and False percentages of the Feature
for each ID
(0 or 1), and I am looking for two output for each ID:
Feature ID Percent
True 1 20%
False 1 30%
Feature ID Percent
True 0 30%
False 0 20%
我尝试了一些尝试,但是我开始获得所有列的计数,然后获得所有列的百分比.
I have tried a few attempts, but I start getting counts for all columns and then a percentage for all columns.
这是我的错误尝试:
percentageID0 = mydf[ mydf['ID']==0 ].set_index(['Feature']).count()
percentageID1 = mydf[ mydf['ID']==1 ].set_index(['Feature']).count()
fullcount = (mydf.groupby(['ID']).count()).sum()
print (percentageID0/fullcount) * 100
print (percentageID1/fullcount) * 100
认为我对groupby/index格式感到困惑.
Think I am getting mixed up with the groupby/index format.
推荐答案
可能就是这样:
In [73]:
print pd.DataFrame({'Percentage': df.groupby(('ID', 'Feature')).size() / len(df)})
Percentage
ID Feature
0 False 0.2
True 0.3
1 False 0.3
True 0.2
这篇关于DataFrame分组依据上的 pandas 百分比计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文