我有三列,它们看起来像这样:
Symbol | Type | Date | Value
----------------------------------------
Reliance | Buy | 01/03/20 | 230000
Reliance | Sell | 04/03/20 | 980000
Reliance | Buy | 17/03/20 | 97000
TCS | Buy | 10/03/20 | 120000
TCS | Buy | 07/03/20 | 890000
TCS | Sell | 05/03/20 | 300000
我想选择最小date
和组value
柱和type
柱。Type列可以显示买入和卖出(一个交易品种只有两个)。
最终结果将如下所示:
Symbol | Type | Date | Value
----------------------------------------
Reliance | Buy | 01/03/20 | 327000
Reliance | Sell | 04/03/20 | 980000
TCS | Buy | 05/03/20 | 1010000
TCS | Sell | 05/03/20 | 300000
到目前为止,我可以在没有日期列的情况下成功分组。代码如下:
new_df = the_df.groupby(['symbol_', type_col])[value_col].sum().reset_index()
我试图这样做:
new_df = the_df.groupby(['symbol_', type_col, date_col])[value_col].sum().reset_index()
df_new = new_df[new_df.groupby('symbol_').date_of_initmation_to_company_.transform('min') == new_df[date_col]]
但是它没有按预期工作。实际上,第二个像with .transform
并没有做任何更改。您能告诉我正确的方法吗?
然后
out = df.groupby(['Symbol','Type']).agg({'Date' : 'first' , 'Value' : 'sum'}).reset_index()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句