pandas.core.base.SpecificationError: nested renamer is not supported
今天跟著老師寫爬蟲的時候發現報了個錯,但是發現代碼也沒錯,搞了半天之后發現是版本的原因
Traceback (most recent call last):
File “E:/code/python/PyCharm/bigdata/class/20201006/爬取豆瓣小說.py”, line 76, in
df_gp = df.groupby(by=[‘等級’])[‘star’].agg({‘人數’: np.size, ‘平均值’: np.mean, ‘最高分’: np.max, ‘最低分’: np.min})
File “E:\environment\Python\Python17-32\lib\site-packages\pandas\core\groupby\generic.py”, line 246, in aggregate
ret = self._aggregate_multiple_funcs(func)
File “E:\environment\Python\Python17-32\lib\site-packages\pandas\core\groupby\generic.py”, line 292, in _aggregate_multiple_funcs
raise SpecificationError(“nested renamer is not supported”)
pandas.core.base.SpecificationError: nested renamer is not supported
最后發現是pandas在 1.0版本后,更新了API寫法——gropuby+agg分組+聚合函數的寫法變了
首先,查看自己的pandas的版本,如果是1版本且報錯如上,則課根據以下方法改
import pandas as pd print (pd.__version__) # 我的是1.1.3報錯的代碼片段
df_gp = df.groupby(by=['等級'])['star'].agg({'人數': np.size, '平均值': np.mean, '最高分': np.max, '最低分': np.min})更改之后的代碼片段
df_gp = df.groupby(by=['等級'])['star'].agg([('人數', np.size), ('平均值', np.mean), ('最高分', np.max), ('最低分', np.min)])總結
以上是生活随笔為你收集整理的pandas.core.base.SpecificationError: nested renamer is not supported的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于JavaStream的一些小练习
- 下一篇: pandas.core.base.Spe