我在下面有一个数据框:
A B C
0 asia 1000 ab
1 africa 2000 ab
2 asia 4000 bc
3 asia 6000 cd
4 USA 200 ab
我想过滤A列= Asia并按C列对B列进行求和。我正在尝试使用:
agg = df[df['A'] = 'asia'].groupby('C')[['B']].sum()
但是它只返回一行B列和C列的总和。我想返回整个数据框。我该如何实现?
尝试这个:
df1 = df[df['A'] == 'asia'].groupby(['A', 'C'], as_index=False).sum()
print(df1)
A C B
0 asia ab 1000
1 asia bc 4000
2 asia cd 6000
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句