【问题标题】:How do I solve SpecificationError: nested renamer is not supported如何解决 SpecificationError:不支持嵌套重命名器
【发布时间】:2021-08-26 19:26:29
【问题描述】:

我正在使用 agg 函数并在我的数据框上使用参数作为字典。但是我得到了不支持嵌套重命名器的错误。我正在执行 google colab notebook 中的代码。

代码:

gb_agg = gb.agg({'Yield' : {'Count' : lambda x: len(x.unique())}})
diff_counties_2013 = gb_agg[gb_agg['Yield']['Count'] > 1].index.get_level_values('CountyName').values
gb_agg[gb_agg['Yield']['Count'] > 1]

错误:

<ipython-input-12-67897b1ec868> in <module>()
      1 
      2 gb = df_2013.groupby(by='CountyName')
----> 3 gb_agg = gb.agg({'Yield' : {'Count' : lambda x: len(x.unique())}})
      4 diff_counties_2013 = gb_agg[gb_agg['Yield']['Count'] > 1].index.get_level_values('CountyName').values
      5 gb_agg[gb_agg['Yield']['Count'] > 1]

1 frames
/usr/local/lib/python3.7/dist-packages/pandas/core/base.py in _aggregate(self, arg, *args, **kwargs)
    336                     # {'ra' : { 'A' : 'mean' }}
    337                     if isinstance(v, dict):
--> 338                         raise SpecificationError("nested renamer is not supported")
    339                     elif isinstance(obj, ABCSeries):
    340                         raise SpecificationError("nested renamer is not supported")

SpecificationError: nested renamer is not supported

【问题讨论】:

  • 您能描述一下您在第一行代码中要实现的目标吗?为什么要重命名 groupby 对象两次?

标签: python pandas dataframe machine-learning pandas-groupby


【解决方案1】:

让我们使用pd.NamedAgg,因为在 pandas 0.25.0 中不推荐使用嵌套重命名器。

df_2013.groupby(by='CountyName').agg(Count=('Yeild','count'))

df_2013.groupby(by='CountyName').agg(Count=('Yeild', lambda x: len(x.unique())))

【讨论】:

  • 我做了你指定的更改,但我没有得到预期的结果
猜你喜欢
  • 1970-01-01
  • 2021-02-07
  • 2022-10-09
  • 2018-05-02
  • 2012-08-07
  • 2016-11-06
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
相关资源
最近更新 更多