define aggfunc for each values column in pandas pivot table

后端 未结 3 905
广开言路
广开言路 2021-01-31 10:11

Was trying to generate a pivot table with multiple \"values\" columns. I know I can use aggfunc to aggregate values the way I want to, but what if I don\'t want to sum or avg bo

3条回答
  •  一个人的身影
    2021-01-31 10:48

    You can concat two DataFrames:

    >>> df1 = pd.pivot_table(df, values=['D'], rows=['B'], aggfunc=np.sum)
    >>> df2 = pd.pivot_table(df, values=['E'], rows=['B'], aggfunc=np.mean)
    >>> pd.concat((df1, df2), axis=1)
              D         E
    B                    
    A  1.810847 -0.524178
    B  2.762190 -0.443031
    C  0.867519  0.078460
    

    or you can pass list of functions as aggfunc parameter and then reindex:

    >>> df3 = pd.pivot_table(df, values=['D','E'], rows=['B'], aggfunc=[np.sum, np.mean])
    >>> df3
            sum                mean          
              D         E         D         E
    B                                        
    A  1.810847 -4.193425  0.226356 -0.524178
    B  2.762190 -3.544245  0.345274 -0.443031
    C  0.867519  0.627677  0.108440  0.078460
    >>> df3 = df3.ix[:, [('sum', 'D'), ('mean','E')]]
    >>> df3.columns = ['D', 'E']
    >>> df3
              D         E
    B                    
    A  1.810847 -0.524178
    B  2.762190 -0.443031
    C  0.867519  0.078460
    

    Alghouth, it would be nice to have an option to defin aggfunc for each column individually. Don't know how it could be done, may be pass into aggfunc dict-like parameter, like {'D':np.mean, 'E':np.sum}.

    update Actually, in your case you can pivot by hand:

    >>> df.groupby('B').aggregate({'D':np.sum, 'E':np.mean})
              E         D
    B                    
    A -0.524178  1.810847
    B -0.443031  2.762190
    C  0.078460  0.867519
    

提交回复
热议问题