将Pandas groupby()+ apply()与参数一起使用 [英] Use Pandas groupby() + apply() with arguments

查看:153
本文介绍了将Pandas groupby()+ apply()与参数一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想结合使用df.groupby()apply()将功能应用于每个组的每一行.

I would like to use df.groupby() in combination with apply() to apply a function to each row per group.

我通常使用下面的代码,这些代码通常可以正常工作(请注意,这没有groupby()):

I normally use the following code, which usually works (note, that this is without groupby()):

df.apply(myFunction, args=(arg1,))

使用groupby()我尝试了以下操作:

With the groupby() I tried the following:

df.groupby('columnName').apply(myFunction, args=(arg1,))

但是,出现以下错误:

TypeError:myFunction()得到了意外的关键字参数'args'

TypeError: myFunction() got an unexpected keyword argument 'args'

因此,我的问题是:如何在需要参数的函数中使用groupby()apply()?

Hence, my question is: How can I use groupby() and apply() with a function that needs arguments?

推荐答案

pandas.core.groupby.GroupBy.apply does NOT have named parameter args, but pandas.DataFrame.apply does have it.

所以尝试一下:

df.groupby('columnName').apply(lambda x: myFunction(x, arg1))

@Zero :

df.groupby('columnName').apply(myFunction, ('arg1'))

演示:

In [82]: df = pd.DataFrame(np.random.randint(5,size=(5,3)), columns=list('abc'))

In [83]: df
Out[83]:
   a  b  c
0  0  3  1
1  0  3  4
2  3  0  4
3  4  2  3
4  3  4  1

In [84]: def f(ser, n):
    ...:     return ser.max() * n
    ...:

In [85]: df.apply(f, args=(10,))
Out[85]:
a    40
b    40
c    40
dtype: int64

使用GroupBy.apply时,您可以传递一个命名参数:

when using GroupBy.apply you can pass either a named arguments:

In [86]: df.groupby('a').apply(f, n=10)
Out[86]:
    a   b   c
a
0   0  30  40
3  30  40  40
4  40  20  30

一个参数元组:

In [87]: df.groupby('a').apply(f, (10))
Out[87]:
    a   b   c
a
0   0  30  40
3  30  40  40
4  40  20  30

这篇关于将Pandas groupby()+ apply()与参数一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆