applying several functions in transform in pandas

后端 未结 3 763
执念已碎
执念已碎 2021-01-14 10:17

After a groupby, when using agg, if a dict of columns:functions is passed, the functions will be applied in the corresponding columns.

3条回答
  •  臣服心动
    2021-01-14 11:13

    With the updates to Pandas, you can use the assign method, along with transform to either append new columns, or replace existing columns with new values :

    grouper = df_test.groupby("a")
    
    df_test.assign(b=grouper["b"].transform("cumsum"), 
                   c=grouper["c"].transform("cumprod"))
    
        a   b   c
    0   1   2   3
    1   1   22  90
    2   2   30  50
    3   1   24  2970
    4   2   34  2500
    

提交回复
热议问题