我有一个如下所示的数据框:
ID TradeDate party Deal Asset Start Expire Fixed Quantity MTM Float
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Buy HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Buy HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Buy WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
我按开始、资产和交易对数据进行分组,然后执行计算将数据帧转换为:
groups = df.groupby(['Start', 'Asset', 'Deal'])
ID TradeDate party Deal Asset Start Expire Fixed Quantity MTM Float
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
total 3000 7500.00
ID TradeDate party Deal Asset Start Expire Fixed Quantity MTM Float
1 04/11/2024 party1 Buy HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Buy HO 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
total 3000 5000.00
ID TradeDate party Deal Asset Start Expire Fixed Quantity MTM Float
1 04/11/2024 party1 Sell WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
1 04/11/2024 party1 Sell WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
total 3000 5000.00
ID TradeDate party Deal Asset Start Expire Fixed Quantity MTM Float
1 04/11/2024 party1 Buy WTI 01/01/2024 02/01/2024 10.00 1000 2500.00 10.00
total 1000 2500.00
我的目标是再次转换这些组,以便我可以只输出我需要的数据。此步骤的预期输出应如下所示:
party Deal Asset Start MTM Float
party1 Sell HO 01/01/2024 7500.00 10.00
party1 Buy HO 01/01/2024 5000.00 10.00
party1 Sell WTI 01/01/2024 5000.00 10.00
party1 Buy WTi 01/01/2024 2500.00 10.00
我需要执行另一次某种分组吗?或者还有其他功能可以实现这一目标吗?任何建议都会有所帮助。
注意:在第二步中,这些是来自列表的单独数据帧。您可能需要一个中间步骤将它们连接在一起,然后得到最终输出。
我认为你只需要
pd.Groupby
+.agg
:印刷: