Pandas GroupBy.agg() 抛出 TypeError:aggregate() 缺少 1 个必需的位置参数:'arg'

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56821579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:24:06  来源:igfitidea点击:

Pandas GroupBy.agg() throws TypeError: aggregate() missing 1 required positional argument: 'arg'

pythonpython-3.xpandasaggregatepandas-groupby

提问by user3476463

I'm trying to create multiple aggregations of the same field. I'm working in pandas, in python3.7. The syntax seems pretty straightforward based on the documentation:

我正在尝试创建同一字段的多个聚合。我在大Pandas工作,在 python3.7 中。根据文档,语法似乎非常简单:

https://pandas-docs.github.io/pandas-docs-travis/user_guide/groupby.html#named-aggregation

https://pandas-docs.github.io/pandas-docs-travis/user_guide/groupby.html#named-aggregation

I do not see why I'm getting the error below. Could someone please point out the issue and tell me how to fix it?

我不明白为什么会收到以下错误。有人可以指出问题并告诉我如何解决吗?

code:

代码:

qt_dy.groupby('date').agg(std_qty=('qty','std'),mean_qty=('qty','mean'),)

error:

错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-62-6bb3aabf313f> in <module>
      5 
      6 qt_dy.groupby('date')\
----> 7 .agg(std_qty=('qty','std'),mean_qty=('qty','mean'))

TypeError: aggregate() missing 1 required positional argument: 'arg'

回答by cs95

Looks like you're trying to use aggwith Named aggregationsthis is a supported feature from v0.25 and above ONLY.

就像你看起来正在尝试使用agg命名聚合-这是从v0.25支持的功能和上面只

For older versions, you will need to use the list of tuples format:

对于旧版本,您需要使用元组列表格式:

qt_dy.groupby('date')['qty'].agg([('std_qty','std'), ('mean_qty','mean')])

Or, to aggregate multiple columns, a dictionary:

或者,要聚合多列,字典:

qt_dy.groupby('date').agg({'qty': [('std_qty','std'), ('mean_qty','mean')]})

For more information, take a look at my answer here.

有关更多信息,请在此处查看我的回答。