Python Pandas:使用参数将多个函数传递给agg()

编程入门 行业动态 更新时间:2024-10-28 05:22:20
本文介绍了Python Pandas:使用参数将多个函数传递给agg()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在努力弄清楚如何为熊猫的dataframe.agg()函数结合两种不同的语法.采取以下简单的数据框架:

I'm struggling to figure out how to combine two different syntaxes for pandas' dataframe.agg() function. 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

我知道您可以将两个函数发送到agg()并获得一个新的数据帧,其中每个函数将应用于每列:

I know you can send two functions to agg() and get a new data frame where each function is applied to each column:

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

但是有没有一种方法可以传递多个函数以及它们中的一个或两个的参数呢?我希望在文档中找到类似df.groupby('A').agg([np.mean, (np.std, ddof=0)])的内容,但到目前为止还没有运气.有什么想法吗?

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

但是,您可以轻松做到:

However, you could easily do:

df.groupby('A').agg([np.mean, lambda x: np.std(x, ddof=0)])

它也将正常工作.

更多推荐

Python Pandas:使用参数将多个函数传递给agg()

本文发布于:2023-10-04 20:24:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1467209.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   函数   参数   Python   Pandas

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!