Pandas GroupBy: apply a function with two arguments

Usually, using the .apply() method, you pass a function that takes exactly one argument.

 def somefunction(group): group['ColumnC'] == group['ColumnC']**2 return group df.groupby(['ColumnA', 'ColumnB']).apply(somefunction) 

Here somefunction is applied for each group , which is then returned. I mainly use this example here .

I want to be able to not specify the ColumnC column ColumnC in advance. Passing it as an argument to somefunction will make the code more flexible.

 def somefunction(group, column_name): group[column_name] == group[column_name]**2 return group df.groupby(['ColumnA', 'ColumnB']).apply(somefunction) 

Is there any way to make this work? I cannot pass group to somefunction because it is magically done .apply() in the background.

+5
source share
2 answers

you can pass keyword arguments via apply

 df.groupby(['ColumnA', 'ColumnB']).apply(somefunction, column_name='col') 

Mcve

 df = pd.DataFrame(dict(A=list(range(2)) * 5, B=range(10)[::-1])) def f(df, arg1): return df * arg1 df.groupby('A').apply(f, arg1=3) AB 0 0 27 1 3 24 2 0 21 3 3 18 4 0 15 5 3 12 6 0 9 7 3 6 8 0 3 9 3 0 
+6
source

You can make an anonymous function

df.groupby(['ColumnA', 'ColumnB']).apply(lambda x: somefunction(x, 'col'))

+1
source

Source: https://habr.com/ru/post/1267144/


All Articles