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?
JavaScript
x
11
11
1
import pandas as pd
2
3
df = pd.DataFrame([[1, 2, 3],
4
[4, 5, 6],
5
[7, 8, 9]],
6
columns=['A', 'B', 'C'])
7
8
pct = lambda x: len(x)/len(df)
9
10
df.groupby('A').agg(pct)
11
returns as expected
JavaScript
1
6
1
B C
2
A
3
1 0.333333 0.333333
4
4 0.333333 0.333333
5
7 0.333333 0.333333
6
But
JavaScript
1
3
1
aggs = {'B':['pct']}
2
df.groupby('A').agg(aggs)
3
returns the following error:
JavaScript
1
2
1
AttributeError: 'SeriesGroupBy' object has no attribute 'pct'
2
Advertisement
Answer
There is string 'pct'
, need variable pct
– lambda function by removing ''
:
JavaScript
1
9
1
aggs = {'B':pct}
2
print(df.groupby('A').agg(aggs))
3
4
B
5
A
6
1 0.333333
7
4 0.333333
8
7 0.333333
9