问题描述
我正在尝试研究如何在 Pandas 中使用 groupby
函数来根据给定的 Yes/No 标准计算出每年值的比例.
I'm trying to work out how to use the groupby
function in pandas to work out the proportions of values per year with a given Yes/No criteria.
例如,我有一个名为 names
的数据框:
For example, I have a dataframe called names
:
Name Number Year Sex Criteria
0 name1 789 1998 Male N
1 name1 688 1999 Male N
2 name1 639 2000 Male N
3 name2 551 1998 Male Y
4 name2 499 1999 Male Y
我可以使用
namesgrouped = names.groupby(["Sex", "Year", "Criteria"]).sum()
获得:
Number
Sex Year Criteria
Male 1998 N 14507
Y 2308
1999 N 14119
Y 2331
等等.我希望数字标准"列显示每个性别和年份的总数百分比 - 因此,对于上面的 1998 年,我将使用 N = 86.27% 和 Y = 13.73%,而不是 N = 14507 和 Y = 2308.
and so on. I would like the 'Number Criteria' column to show the % of the total for each gender and year - so instead of N = 14507 and Y = 2308 for 1998 above I'd have N = 86.27% and Y = 13.73%.
谁能建议如何做到这一点?
Can anyone advise how to do this?
推荐答案
这个问题是建议重复.借用接受的答案,这将起作用:
This question is a direct extension of the suggested duplicate. Borrowing from the accepted answer, this will work:
In [46]: namesgrouped.groupby(level=[0, 1]).apply(lambda g: g / g.sum())
Out[46]:
Number
Sex Year Criteria
Male 1998 N 0.588806
Y 0.411194
1999 N 0.579612
Y 0.420388
2000 N 1.000000
编辑:转换操作可能比应用更快:
Edit: a transform operation might be faster than apply:
namesgrouped / namesgrouped.groupby(level=[0, 1]).transform('sum')
这篇关于如何在pandas中使用groupby根据另一列中的条件计算百分比/比例总计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!