我有一个python脚本,它从csv文件中读入数据
代码运行得很好,但每次运行我都会收到这样的警告消息:
FutureWarning: A grouping was used that is not in the columns of the DataFrame and so was excluded from the result. This grouping will be included in a future version of pandas. Add the grouping as a column of the DataFrame to silence this warning.
这条消息让我看到了这个代码:
formatted_df = {k: 'first' for k in df.columns} | {'Commodity': tuple, 'FixedPriceStrike': tuple, 'Quantity': tuple}
group = (df['TradeID'].str.strip().ne('') | df['TradeDate'].str.strip().ne('')).cumsum()
df = df.groupby(group, as_index=False).agg(formatted_df)
以下是一些示例数据:
TradeID TradeDate Commodity StartDate ExpiryDate FixedPrice Quantity MTMValue
-------- ---------- --------- --------- ---------- ---------- -------- ---------
aaa 01/01/2024 commodity1 01/01/2024 01/01/2024 10.00 10 100.00
commodity2 10.00 10
bbb 01/01/2024 commodity1 01/01/2024 01/01/2024 10.00 10 100.00
commodity2 10.00 10
ccc 01/01/2024 commodity1 01/01/2024 01/01/2024 10.00 10 100.00
commodity2 10.00 10
这里是预期的输出:
TradeID TradeDate Commodity StartDate ExpiryDate FixedPrice Quantity MTMValue
-------- ---------- --------- --------- ---------- ---------- -------- ---------
aaa 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 (10,10) (10,10) 100.00
bbb 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 (10,10) (10,10) 100.00
ccc 01/01/2024 (com1,com2) 01/01/2024 01/01/2024 (10,10) (10,10) 100.00
我不确定如何消除这个警告,因为我不想只将它告诉我的任何分组添加到数据帧中.对于如何消除此警告或采用不同的方法获得所需输出,有什么建议吗?