首页 > 解决方案 > 数据透视表函数不起作用 - 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'

提前致谢。

数据集的第一列 数据集的 最后一列

标签: python-3.xpandasnumpypivotpivot-table

解决方案


推荐阅读