Home > OS >  pivot_table(), multiple aggfuncs to the *same* column - possible?
pivot_table(), multiple aggfuncs to the *same* column - possible?

Time:02-11

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?

CodePudding user response:

IIUC use:

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