Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas GroupBy: apply a function with two arguments

Tags:

python

pandas

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.

like image 865
Michael Gecht Avatar asked Apr 25 '17 16:04

Michael Gecht


People also ask

How do I apply a function to multiple columns in pandas?

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).

How do pandas use two aggregate functions?

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.


1 Answers

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
like image 198
piRSquared Avatar answered Sep 24 '22 16:09

piRSquared