Usually when using the .apply()
method, one passes 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. Basically I'm using this example here.
I want to have the ability to not specify the column name ColumnC
beforehand. Passing it along as an argument of somefunction
would 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 can't pass group
to somefunction
, because that is magically done by .apply()
in the background.
Pandas apply() Function to Single & Multiple Column(s) Using pandas. DataFrame. apply() method you can execute a function to a single column, all and list of multiple columns (two or more).
To apply aggregations to multiple columns, just add additional key:value pairs to the dictionary. Applying multiple aggregation functions to a single column will result in a multiindex. Working with multi-indexed columns is a pain and I'd recommend flattening this after aggregating by renaming the new columns.
you can pass key word arguments through 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)
A B
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With