Use Pandas groupby() + apply() with arguments Use Pandas groupby() + apply() with arguments python python

Use Pandas groupby() + apply() with arguments


pandas.core.groupby.GroupBy.apply does NOT have named parameter args, but pandas.DataFrame.apply does have it.

So try this:

df.groupby('columnName').apply(lambda x: myFunction(x, arg1))

or as suggested by @Zero:

df.groupby('columnName').apply(myFunction, ('arg1'))

Demo:

In [82]: df = pd.DataFrame(np.random.randint(5,size=(5,3)), columns=list('abc'))In [83]: dfOut[83]:   a  b  c0  0  3  11  0  3  42  3  0  43  4  2  34  3  4  1In [84]: def f(ser, n):    ...:     return ser.max() * n    ...:In [85]: df.apply(f, args=(10,))Out[85]:a    40b    40c    40dtype: int64

when using GroupBy.apply you can pass either a named arguments:

In [86]: df.groupby('a').apply(f, n=10)Out[86]:    a   b   ca0   0  30  403  30  40  404  40  20  30

a tuple of arguments:

In [87]: df.groupby('a').apply(f, (10))Out[87]:    a   b   ca0   0  30  403  30  40  404  40  20  30


Some confusion here over why using an args parameter throws an error might stem from the fact that pandas.DataFrame.apply does have an args parameter (a tuple), while pandas.core.groupby.GroupBy.apply does not.

So, when you call .apply on a DataFrame itself, you can use this argument; when you call .apply on a groupby object, you cannot.

In @MaxU's answer, the expression lambda x: myFunction(x, arg1) is passed to func (the first parameter); there is no need to specify additional *args/**kwargs because arg1 is specified in lambda.

An example:

import numpy as npimport pandas as pd# Called on DataFrame - `args` is a 1-tuple# `0` / `1` are just the axis arguments to np.sumdf.apply(np.sum, axis=0)  # equiv to df.sum(0)df.apply(np.sum, axis=1)  # equiv to df.sum(1)# Called on groupby object of the DataFrame - will throw TypeErrorprint(df.groupby('col1').apply(np.sum, args=(0,)))# TypeError: sum() got an unexpected keyword argument 'args'


For me

df2 = df.groupby('columnName').apply(lambda x: my_function(x, arg1, arg2,))

worked