pandas GroupBy.agg()引发TypeError:aggregate()缺少1个必需的位置参数:'arg' [英] Pandas GroupBy.agg() throws TypeError: aggregate() missing 1 required positional argument: 'arg'
问题描述
我正在尝试创建同一字段的多个聚合.我正在使用python3.7在熊猫中工作.根据文档,语法似乎非常简单:
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:
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?
代码:
qt_dy.groupby('date').agg(std_qty=('qty','std'),mean_qty=('qty','mean'),)
错误:
---------------------------------------------------------------------------
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'
推荐答案
Looks like you're trying to use agg
with Named aggregations—this is a supported feature from v0.25 and above ONLY.
对于旧版本,您将需要使用元组格式列表:
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.
这篇关于 pandas GroupBy.agg()引发TypeError:aggregate()缺少1个必需的位置参数:'arg'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!