![](/img/trans.png)
[英]Apply condition on a column after groupby in pandas and then aggregate to get 2 max value
[英]Groupby, aggregate and check the condition in Pandas
我应该汇总每个国家/地区的数字列中的值并检查它是否大于或等于 2。如果为真,它应该在我的数据中显示为一列。
数据集
Country Number bool
India 1 yes
India 0 no
India 2 no
India 0 yes
Germany 1 no
Germany 0 no
Germany 0 yes
Japan 2 yes
Japan 0 yes
预期产出
Country Number bool result
India 1 yes 3
India 0 no 3
India 2 no 3
India 0 yes 3
Japan 2 yes 2
Japan 0 yes 2
谢谢!
因此,在您的情况下,请使用groupby
进行trasnform
,然后对其进行过滤
df['result'] = df.groupby('Country')['Number'].transform('sum')
df = df.query('result>=2')
df
Out[18]:
Country Number bool result
0 India 1 yes 3
1 India 0 no 3
2 India 2 no 3
3 India 0 yes 3
7 Japan 2 yes 2
8 Japan 0 yes 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.