Alec McKay
Alec McKay

Reputation: 135

Modify aggfunctions in pivot_tables

How can I combine two or more aggfunctions in a pandas pivot table?

I want to do something like:

pt = pandas.pivot_table(data, index=['Name'], values=['Grades'], aggfunc=[np.max - np.min])

To get the difference between the max and the min.

Likewise, is there any way to modify the aggfunc with a constant? Say doing something like:

...aggfunc=[np.max - 0.5])

(I know all of this could be done by manipulating the underlying data. But this would be a lot easier)

Upvotes: 0

Views: 180

Answers (1)

Dani Mesejo
Dani Mesejo

Reputation: 61910

You can pass any Python function to the parameter aggfunc of pandas.pivot_table, from the documentation:

aggfunc : function, list of functions, dict, default numpy.mean

As an example, for your first case, you could do:

Setup

import numpy as np
import pandas as pd

data = pd.DataFrame(
    {'Name': ['bob', 'alice', 'mary', 'tom', 'bob', 'alice', 'mary', 'tom'], 'Grades': [10, 9, 8, 8, 7, 5, 6, 8]})

Code

result = pd.pivot_table(data, index=['Name'], values=['Grades'], aggfunc=lambda x: np.max(x) - np.min(x))

print(result)

Output

       Grades
Name         
alice       4
bob         3
mary        2
tom         0

Upvotes: 1

Related Questions