python-3.x - 数据透视表函数不起作用 - AttributeError:“numpy.ndarray”对象没有属性“名称”
问题描述
数据集的第一列 数据集的 最后一列 不确定此错误的含义。我正在为我的数据集创建一个数据透视表,即加拿大劳动力调查和就业统计的详细信息随时间变化),该数据集适用于每年和省的组合,特别是一个除外。
我正在运行的代码是:
no_row_df = pd.pivot_table(no_row_data, values = "FINALWT", index = ["SURVDATE", "PROV"], aggfunc=np.sum)
for col in count_columns:
table = pd.pivot_table(no_row_data, values = "FINALWT", index = ["SURVDATE", "PROV"], columns=[col], aggfunc=np.sum)
no_row_df = df.merge(table, left_index=True, right_on=["SURVDATE", "PROV"])
no_row_df
其中 SURVDATE 是调查日期(年/月形式),PROV 是省。FINALWT 是权重,代表具有特定劳动特征的个人数量。有一大堆专栏,但它们基本上归结为行业、职业、公司规模等。
完整的错误是:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-17-ade72c6b3106> in <module>
2 for col in count_columns:
3 table = pd.pivot_table(no_row_data, values = "FINALWT", index = ["SURVDATE", "PROV"], columns=[col], aggfunc=np.sum)
----> 4 no_row_df = df.merge(table, left_index=True, right_on=["SURVDATE", "PROV"])
5 no_row_df
~/anaconda3/lib/python3.7/site-packages/pandas/core/frame.py in merge(self, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
7347 copy=copy,
7348 indicator=indicator,
-> 7349 validate=validate,
7350 )
7351
~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
81 validate=validate,
82 )
---> 83 return op.get_result()
84
85
~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in get_result(self)
665 result = self._indicator_post_merge(result)
666
--> 667 self._maybe_add_join_keys(result, left_indexer, right_indexer)
668
669 self._maybe_restore_index_levels(result)
~/anaconda3/lib/python3.7/site-packages/pandas/core/reshape/merge.py in _maybe_add_join_keys(self, result, left_indexer, right_indexer)
819 elif result._is_level_reference(name):
820 if isinstance(result.index, MultiIndex):
--> 821 key_col.name = name
822 idx_list = [
823 result.index.get_level_values(level_name)
AttributeError: 'numpy.ndarray' object has no attribute 'name'
提前致谢。
解决方案
推荐阅读
- node.js - 猫鼬,删除后响应不正确
- reactjs - 为什么页面刷新按钮点击反应?
- excel - 以变量/单元格为标准的自动过滤器
- php - 如何在查询字符串中捕获受保护页面的 url?
- php - 如何在多维数组中查找重复条目的键
- node.js - 如何修复'ffmpeg服务器返回403禁止(访问被拒绝)?
- amazon-redshift - to_timestamp 匹配列值并从 Redshift 检索记录
- python - 如何在 Pandas DataFrame 中获取 2 个列表的差异?
- javascript - 在 DateRangePicker 中将 minDate 设置为当前日期
- javascript - 为什么我在运行 myqsl 查询时在 nodeJS 中收到“将循环结构转换为 JSON”错误?