Sum product and groupby(对产品和分组求和)
本文介绍了对产品和分组求和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个如下所示的数据框:
I have a dataframe that looks like this:
allHoldingsFund
allHoldingsFund
BrokerBestRate notional_current DistanceBestRate
0 CITI 7.859426e+05 0.023194
1 WFPBS 3.609674e+06 -0.023041
2 WFPBS 1.488828e+06 -0.023041
3 JPM 3.484168e+05 -0.106632
4 CITI 6.088499e+05 0.023194
5 WFPBS 8.665558e+06 -0.023041
6 WFPBS 4.219563e+05 -0.023041
我正在尝试一次完成求和积和分组(不创建额外的求和积列)
I am trying to do a sum product and a group by in one go (without creating an extra column of sum product)
这行代码我试过了
allHoldingsFund.groupby(['BrokerBestRate'])['notional_current']*['DistanceBestRate'].sum()
如何进行求和,然后使用 group by 进行聚合?
how can I do a sum product and then aggregate it using group by?
期望的输出
BrokerBestRate product of (notional_current and DistanceBestRate)
CITI 654654645665466
JPM 453454534545367
WFPBS 345345345345435
非常感谢
推荐答案
可以在groupby之前建product栏
You can build the product column before the groupby
df.assign(col=df.notional_current*df.DistanceBestRate).groupby('BrokerBestRate',as_index=False).col.sum()
Out[372]:
BrokerBestRate col
0 CITI 32350.817245
1 JPM -37152.380218
2 WFPBS -326860.001568
这篇关于对产品和分组求和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
织梦狗教程
本文标题为:对产品和分组求和
基础教程推荐
猜你喜欢
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
