Reputation: 811
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
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
Reputation: 34086
Try this:
X.groupby('CHAS')['CRIM'].agg([lambda x: np.percentile(x, 50),'sum', 'count'])
Upvotes: 2