python - Pandas GroupBy.agg() 抛出 TypeError : aggregate() missing 1 required positional argument: 'arg'

标签 python python-3.x pandas aggregate pandas-groupby

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

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'

最佳答案

看起来您正在尝试将 aggNamed 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/

相关文章:

python - Python 中存在 MemoryError,但 IPython 中没有

python-3.x - 从 flask 中的 before_request() 返回

python - 在 python 中比较数据帧的两列时出现错误结果

python - Pandas - 取消堆叠/使用多个索引进行透视

python - 使用 pandas.read_csv() 处理读取数据时的异常

python - 如何匹配模式并向其添加字符

python - Sphinx 找不到我的 python 文件。说 'no module named ...'

python - 最大化平方和模 m 的代码

python-3.x - 在自动调整大小的窗口中选择ROI

Python:使用 np.where 维护多列