简体   繁体   中英

Pandas GroupBy: apply a function with two arguments

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.

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

你可以创建一个匿名函数

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM