Apply multiple functions to multiple groupby columns

前端 未结 7 2177
春和景丽
春和景丽 2020-11-22 03:16

The docs show how to apply multiple functions on a groupby object at a time using a dict with the output column names as the keys:

In [563]: grouped[\'D\'].a         


        
7条回答
  •  一整个雨季
    2020-11-22 04:01

    Pandas >= 0.25.0, named aggregations

    Since pandas version 0.25.0 or higher, we are moving away from the dictionary based aggregation and renaming, and moving towards named aggregations which accepts a tuple. Now we can simultaneously aggregate + rename to a more informative column name:

    Example:

    df = pd.DataFrame(np.random.rand(4,4), columns=list('abcd'))
    df['group'] = [0, 0, 1, 1]
    
              a         b         c         d  group
    0  0.521279  0.914988  0.054057  0.125668      0
    1  0.426058  0.828890  0.784093  0.446211      0
    2  0.363136  0.843751  0.184967  0.467351      1
    3  0.241012  0.470053  0.358018  0.525032      1
    

    Apply GroupBy.agg with named aggregation:

    df.groupby('group').agg(
                 a_sum=('a', 'sum'),
                 a_mean=('a', 'mean'),
                 b_mean=('b', 'mean'),
                 c_sum=('c', 'sum'),
                 d_range=('d', lambda x: x.max() - x.min())
    )
    
              a_sum    a_mean    b_mean     c_sum   d_range
    group                                                  
    0      0.947337  0.473668  0.871939  0.838150  0.320543
    1      0.604149  0.302074  0.656902  0.542985  0.057681
    

提交回复
热议问题