How to aggregate multiple columns in pandas groupby

How to aggregate multiple columns in pandas groupby,第1张

How to aggregate multiple columns in pandas groupby

Pretty much just write it like your pseudopre.

In [14]: s = mn.groupby(['keyA','state'], as_index=False).sum()In [15]: s['v1'] = s['n1'] / s['d1']In [16]: s['v2'] = s['n2'] / s['d2']In [17]: s[['keyA', 'state', 'v1', 'v2']]Out[17]:    keyA state   v1v20  key1    AL  100   500.0000001  key1    CA  100   500.0000002  key1    NY  100  1500.0000003  key2    CA  100  1166.6666674  key2    FL  100  1166.666667[5 rows x 4 columns]

I think you have a typo in your example data by the way. The second

n1

header should be
n2
.



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5673354.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存