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.
source share