I'm trying to pass a user defined function pct
to Pandas agg
method, and it works if I only pass that function but it doesn't when I use the dictionary format for defining the functions. Does anyone know why?
import pandas as pd
df = pd.DataFrame([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]],
columns=['A', 'B', 'C'])
pct = lambda x: len(x)/len(df)
df.groupby('A').agg(pct)
returns as expected
B C
A
1 0.333333 0.333333
4 0.333333 0.333333
7 0.333333 0.333333
But
aggs = {'B':['pct']}
df.groupby('A').agg(aggs)
returns the following error:
AttributeError: 'SeriesGroupBy' object has no attribute 'pct'