Skip to content
Advertisement

Why Pandas gives AttributeError: ‘SeriesGroupBy’ object has no attribute ‘pct’?

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'

Advertisement

Answer

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
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement