Python Pandas如何将groupby *** 作结果分配回父数据帧中的列?

Python Pandas如何将groupby *** 作结果分配回父数据帧中的列?,第1张

Python Pandas如何将groupby *** 作结果分配回父数据帧中的列?
In [97]: df = pandas.Dataframe({'month': np.random.randint(0,11, 100), 'A': np.random.randn(100), 'B': np.random.randn(100)})In [98]: df.join(df.groupby('month')['A'].sum(), on='month', rsuffix='_r')Out[98]:A         B  month       A_r0  -0.040710  0.182269      0 -0.3318161  -0.004867  0.642243      1  2.4482322  -0.162191  0.442338      4  2.0459093  -0.979875  1.367018      5 -2.7363994  -1.126198  0.338946      5 -2.7363995  -0.992209 -1.343258      1  2.4482326  -1.450310  0.021290      0 -0.3318167  -0.675345 -1.359915      9  2.722156


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

原文地址: http://outofmemory.cn/zaji/5645766.html

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

发表评论

登录后才能评论

评论列表(0条)

保存