对产品和分组求和
- 作者: 锐63094547
- 来源: 51数据库
- 2022-10-24
问题描述
我有一个如下所示的数据框:
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
推荐阅读
热点文章
Discord.py(重写)on_member_update 无法正常工作
0
Discord.py 在 vc 中获取用户分钟数
0
discord.py 重写 |为我的命令出错
0
Discord.py rewrite 如何 DM 命令?
0
播放音频时,最后一部分被切断.如何解决这个问题?(discord.py)
0
在消息删除消息 Discord.py
0
如何使 discord.py 机器人私人/直接消息不是作者的人?
0
(Discord.py) 如何获取整个嵌入内容?
0
Discord bot 尽管获得了许可,但不能提及所有人
0
Discord.py discord.NotFound 异常
0
