[英]Groupby, aggregate and check the condition in Pandas
I am supposed to aggregate the values in number column for each country and check if it is greater than or equal to 2. If true, it should be displayed as a column in my data.我应该汇总每个国家/地区的数字列中的值并检查它是否大于或等于 2。如果为真,它应该在我的数据中显示为一列。
Dataset数据集
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
Expected Output预期产出
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
Thanks!谢谢!
So in your case do trasnform
with groupby
then filter it因此,在您的情况下,请使用
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.