【发布时间】: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