Carlos Mougan
Carlos Mougan

Reputation: 811

Pandas groupby and custom agg lambda function

I have a data frame and I want to aggregate a custom aggregation function.

Right now I have it like a predefined function, but I want to call it as a lambda function. Notice that the predefined function has a parameter that can be change.

from sklearn.datasets import load_boston
import pandas as pd
import numpy as np
bunch = load_boston()

y = bunch.target
X = pd.DataFrame(bunch.data, columns=bunch.feature_names)

def percentile_func(y,PERCENTILE=50):
    return np.percentile(y,PERCENTILE)

X.groupby('CHAS')['CRIM'].agg([percentile_func,'sum', 'count'])

Upvotes: 1

Views: 3323

Answers (2)

Ch3steR
Ch3steR

Reputation: 20659

You can use functools.partial here.

from functools import partial

f = partial(percentile_func,PERCENTILE=50) # you can change PERCENTILE value accordingly.

X.groupby('CHAS')['CRIM'].agg([f,'sum', 'count'])

Upvotes: 1

Mayank Porwal
Mayank Porwal

Reputation: 34086

Try this:

X.groupby('CHAS')['CRIM'].agg([lambda x: np.percentile(x, 50),'sum', 'count'])

Upvotes: 2

Related Questions