我之前已经发布过这个问题,但由于提出了类似的问题,它一直被关闭,但这些解决方案在这里对我没有帮助。
我有一个数据框,需要按 3 个不同的列进行分组。根据结果分组,我需要执行计算,然后将结果应用到新列中的每一行。
我的数据如下所示:
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue
---- ----- ----- --------- --------- ------- ---------------- -------- ---------
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 5 25.00
我的目标是按 [交易、商品、开始日期] 对数据进行分组,以便结果数据如下所示:
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue
---- ----- ----- --------- --------- ------- ---------------- -------- ---------
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue
---- ----- ----- --------- --------- ------- ---------------- -------- ---------
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 5 25.00
由此,我需要使用公式来计算“fprice”并将其添加到每一行,如下所示:
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue fprice
---- ----- ----- --------- --------- ------- ---------------- -------- --------- -----
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 0
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 0
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue fprice
---- ----- ----- --------- --------- ------- ---------------- -------- --------- -----
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00 1.25
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 25.00 1.25
我的问题在于下一步,当我尝试将 fprice 添加回原始数据帧时,我有这行代码:
df['fprice'] = df.groupby(['StartDate', 'Commodity', 'Deal']).apply(lambda group: -(group['MTMValue'].sum() - (group['FixedPriceStrike'] * group['Quantity']).sum()) / group['Quantity'].sum()).reset_index(drop=True)
返回此数据框:
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue fprice
---- ----- ----- --------- --------- ------- ---------------- -------- --------- -----
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 0
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 1.25
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 25.00
当结果应该看起来像
ID Deal Party Commodity startdate enddate fixedpricestrike quantity mtmvalue fprice
---- ----- ----- --------- --------- ------- ---------------- -------- --------- -----
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 0
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00 0
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00 1.25
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 25.00 1.25
我对使用 pandas 也比较陌生,我不确定为什么我的结果会这样。任何建议都会有所帮助
您可以先计算“fprice”,然后将其合并回原始 DataFrame,而不是一步完成: