[英]Python Pandas: Passing Multiple Functions to agg() with Arguments
I'm struggling to figure out how to combine two different syntaxes for pandas' dataframe.agg()
function. 我正在努力弄清楚如何为pandas的
dataframe.agg()
函数组合两种不同的语法。 Take this simple data frame: 拿这个简单的数据框:
df = pd.DataFrame({'A': ['group1', 'group1', 'group2', 'group2', 'group3', 'group3'],
'B': [10, 12, 10, 25, 10, 12],
'C': [100, 102, 100, 250, 100, 102]})
>>> df
[output]
A B C
0 group1 10 100
1 group1 12 102
2 group2 10 100
3 group2 25 250
4 group3 10 100
5 group3 12 102
I know you can send two functions to agg()
and get a new data frame where each function is applied to each column: 我知道你可以向
agg()
发送两个函数并获得一个新的数据框,其中每个函数都应用于每一列:
df.groupby('A').agg([np.mean, np.std])
[output]
B C
mean std mean std
A
group1 11.0 1.414214 101 1.414214
group2 17.5 10.606602 175 106.066017
group3 11.0 1.414214 101 1.414214
And I know you can pass arguments to a single function: 我知道你可以将参数传递给一个函数:
df.groupby('A').agg(np.std, ddof=0)
[output]
B C
A
group1 1.0 1
group2 7.5 75
group3 1.0 1
But is there a way to pass multiple functions along with arguments for one or both of them? 但有没有办法传递多个函数以及它们中的一个或两个的参数? I was hoping to find something like
df.groupby('A').agg([np.mean, (np.std, ddof=0)])
in the docs, but so far no luck. 我希望在
df.groupby('A').agg([np.mean, (np.std, ddof=0)])
找到类似df.groupby('A').agg([np.mean, (np.std, ddof=0)])
的内容,但到目前为止还没有运气。 Any ideas? 有任何想法吗?
Well, the docs on aggregate are in fact a bit lacking. 好吧,关于聚合的文档实际上有点缺乏。 There might be a way to handle this with the correct passing of arguments, and you could look into the source code of pandas for that (perhaps I will later).
可能有一种方法可以通过正确传递参数来处理这个问题,你可以查看pandas的源代码(也许我会稍后)。
However, you could easily do: 但是,你可以很容易地做到:
df.groupby('A').agg([np.mean, lambda x: np.std(x, ddof=0)])
And it would work just as well. 它也会起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.