Reputation: 3
I defined the following user function:
def group_by(df, columns):
x = df.groupby(columns).sum()
x = x.reset_index()
return x
It works!
But the following does not! :
def group_by(df, columns, aggfunc=sum):
x = df.groupby(columns).aggfunc()
x = x.reset_index()
return x
I get the error:
AttributeError: 'DataFrameGroupBy' object has no attribute 'aggfunc'
Any ideas why?
Upvotes: 0
Views: 180
Reputation: 3686
When doing df.groupby(columns).aggfunc()
you are doing a method call on the groupby object, this means Python will look for a method called aggfunc on this object. As this method does not exists it throws an AttributeError. The fact that you have aggfunc as an input parameter of your function doesn't affect this process.
You could however use the .agg() method to which you could pass the local variable aggfunc you have defined as input parameter.
x = df.groupby(columns).agg(aggfunc)
Upvotes: 1