gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:06:44 27 4
gpt4 key购买 nike

我正在尝试创建同一字段的多个聚合。我在 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com