Multiple aggregations of the same column using pandas GroupBy.agg()

前端 未结 3 2217
太阳男子
太阳男子 2020-11-22 11:32

Is there a pandas built-in way to apply two different aggregating functions f1, f2 to the same column df[\"returns\"], without having to call

3条回答
  •  甜味超标
    2020-11-22 12:03

    You can simply pass the functions as a list:

    In [20]: df.groupby("dummy").agg({"returns": [np.mean, np.sum]})
    Out[20]:         
               mean       sum
    dummy                    
    1      0.036901  0.369012
    

    or as a dictionary:

    In [21]: df.groupby('dummy').agg({'returns':
                                      {'Mean': np.mean, 'Sum': np.sum}})
    Out[21]: 
            returns          
               Mean       Sum
    dummy                    
    1      0.036901  0.369012
    

提交回复
热议问题