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'
There is string 'pct'
, need variable pct
- lambda function by removing ''
:
aggs = {'B':pct}
print(df.groupby('A').agg(aggs))
B
A
1 0.333333
4 0.333333
7 0.333333
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.