Skip to content Skip to sidebar Skip to footer

Multiple Aggfun In Pandas

If you think about pivot tables in Excel, you can add additional columns and change from sum to mean to min or max. Is it possible to get the multiple values in a pivot in Pandas?

Solution 1:

You can pass a list to pivot_table's aggfunc keyword argument:

>>> pd.pivot_table(df, values=['D', 'E'], rows=['B'], aggfunc=[np.mean, np.sum])
       meansumDEDEBA-0.1024030.854174-0.8192246.833389B0.426928-0.1773443.415428-1.418754C-0.159123-0.071418-1.272980-0.571341[3 rows x 4 columns]

(PS: you can also use the method version, i.e. df.pivot_table(stuff).)

Post a Comment for "Multiple Aggfun In Pandas"