1

EDIT - please don't lock this question, the similar question links do not provide answers as my question is for applying multiple aggfuncs to the same value column.

I have a standard pivot_table() function being applied to a dataframe in the following way:

pivot = df.pivot_table(index=['Year', 'Month'], values=['Claims', 'Policy Holdings'], aggfunc={'Claims': 'min', 'Policy Holdings': 'max'})

I'm interested to know, say that I want to use the same column in my values twice, with a different aggfunc (i.e. min and max) - is this possible?

The following code doesn't work:

pivot = df.pivot_table(index=['Year', 'Month'], values=['Claims', 'Claims'], aggfunc={'Claims': 'min', 'Claims': 'max'})

Of course I could make a copy of the column beforehand with a different name but I imagine there's a more elegant solution available?

  • `Groupby` then `agg` and `pivot_table` are pretty similar. You can find useful information [here](https://stackoverflow.com/q/12589481/15239951) – Corralien Feb 10 '22 at 10:29

1 Answers1

4

IIUC use:

pivot = df.pivot_table(index=['Year', 'Month'], values=['Claims'], aggfunc={'Claims': ['min','max']})
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252