define aggfunc for each values column in pandas pivot table

后端 未结 3 883
广开言路
广开言路 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 11:02

    table = pivot_table(df, values=['D', 'E'], index=['A', 'C'],
                    aggfunc={'D': np.mean,'E': np.sum})
    

    table D E mean sum A C bar large 5.500000 7.500000 small 5.500000 8.500000 foo large 2.000000 4.500000 small 2.333333 4.333333

提交回复
热议问题