13

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'
Franco Piccolo
  • 6,845
  • 8
  • 34
  • 52
  • 1
    Your function is `pct` not `'pct'`, which is a string which `.agg()` won't know how to associate with a function. – smci Feb 10 '20 at 03:38

1 Answers1

14

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
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252