本文介紹了對(duì)產(chǎn)品和分組求和的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!
問題描述
我有一個(gè)如下所示的數(shù)據(jù)框:
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
我正在嘗試一次完成求和積和分組(不創(chuàng)建額外的求和積列)
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()
如何進(jìn)行求和,然后使用 group by 進(jìn)行聚合?
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
這篇關(guān)于對(duì)產(chǎn)品和分組求和的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!