我正在尝试创建同一字段的多个聚合。我正在使用python3.7在 Pandas 中工作。根据文档,语法似乎非常简单:
https://pandas-docs.github.io/pandas-docs-travis/user_guide/groupby.html#named-aggregation
我看不到以下错误的原因。有人可以指出这个问题并告诉我如何解决吗?
代码:
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'
最佳答案
看起来您正在尝试将agg
与Named aggregations一起使用— ,这是v0.25及更高版本中受支持的功能,仅。
对于较旧的版本,您将需要使用元组格式列表:
qt_dy.groupby('date')['qty'].agg([('std_qty','std'), ('mean_qty','mean')])
或者,要聚合多个列,可以使用一个字典:
qt_dy.groupby('date').agg({'qty': [('std_qty','std'), ('mean_qty','mean')]})
有关更多信息,请看我的答案here。
关于python - Pandas GroupBy.agg()引发TypeError : aggregate() missing 1 required positional argument: 'arg' ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56821579/