如何在“groupby”上执行操作,例如美元重量和其他

2024-09-30 12:32:49 发布

您现在位置:Python中文网/ 问答频道 /正文

全部,

我有一个DF,看起来像这样:

allHoldingsLookUp[['carry_dt','sedol','notional_current','carry_rate']]

        carry_dt    sedol  notional_current  carry_rate
21579 2018-05-18  B27WYK0      1.476290e+05       75.00
21659 2018-05-18  6433912      2.029608e+02       40.00
21992 2018-05-18  2386009      3.002272e+05      100.00
22453 2018-05-18  B671GT8      1.610390e+06       75.00
22700 2018-05-18  6350260      2.582623e+06       55.00
22722 2018-05-18  6418801      1.947338e+06       75.00
22839 2018-05-18  6683416      3.062509e+02      600.00
22917 2018-05-18  6180230      2.594927e+06       40.67
23011 2018-05-18  7302215      2.774702e+05      125.00
23043 2018-05-18  B04QR13      1.113231e+06       60.00
23069 2018-05-18  BYXZ2W5      4.182451e+05      100.00
23120 2018-05-18  6444066      9.507816e+05       35.00
23136 2018-05-18  6683449      1.701068e+06       35.00
23476 2018-05-18  BDVLJ72      1.004499e+07       40.00
23491 2018-05-18  BDVLJ72      1.875754e+05      200.00
23533 2018-05-18  B73XCZ3      3.313149e+02       40.00
23595 2018-05-18  BF20GG0      6.167594e+05      150.00
23620 2018-05-18  B671GT8      9.426840e+05       50.00
23674 2018-05-18  2599131      5.370326e+04      500.00
23824 2018-05-18  6568610      2.244832e+06       35.00
23831 2018-05-18  2311120      1.158353e+06       50.00
23836 2018-05-18  B04QR13      6.560241e+05      125.00
23914 2018-05-18  2599131      1.127563e+06      100.00
23926 2018-05-18  B3L2NF1      2.280907e+06       42.51
24014 2018-05-18  6771645      2.457539e+06       40.00
24067 2018-05-18  6536684      2.954588e+05      100.00
24238 2018-05-18  B613DJ9      1.055963e+06       47.00
24240 2018-05-18  2458771      6.187657e+02       20.00
24294 2018-05-18  6490917      2.855108e+06       35.00
24414 2018-05-18  2639349      1.483675e+05       50.00

例如,现在我通常很容易进行标准分组操作

allHoldingsLookUp.groupby(['carry_dt', 'sedol'], as_index=False)['carry_rate'].agg({"availMin": "min", "availSum":lambda x: sum(x)})

现在,我想在groupby上执行一个操作,涉及另一列“名义上的_current”

我如何“名义”加权“携带率”列?i、 e

在excel中,它与此类似

sumproduct(allHoldingsLookUp['notional_current'],allHoldingsLookUp['carry_rate'])/sum(allHoldingsLookUp['notional_current'])

更一般地说,如何通过涉及另一列来对“groupby”执行操作

非常感谢


Tags: dfratedtcurrentsumgroupby名义carry

热门问题