枢轴:ValueError:索引包含重复的条目,无法重塑

时间:2018-10-05 12:01:51

标签: python pandas pivot-table

在清洗和预处理后,我想在给定的最终表中的标签和用户名之间绘制热图。 出现以下错误。

我已经粘贴了完整的错误,我正在搜索类似的StackOverflow错误,但无法获得正确的结果。

enter image description here

final_sns = final.pivot("hashtags", "username") ax = sns.heatmap(final_sns)

ValueError                                Traceback (most recent call last)
<ipython-input-51-277e0506604d> in <module>()
----> 1 final_sns = final.pivot("hashtags", "username")
      2 ax = sns.heatmap(final_sns)

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\frame.py in pivot(self, index, columns, values)
   5192         """
   5193         from pandas.core.reshape.reshape import pivot
-> 5194         return pivot(self, index=index, columns=columns, values=values)
   5195 
   5196     _shared_docs['pivot_table'] = """

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\reshape\reshape.py in pivot(self, index, columns, values)
    413             indexed = self._constructor_sliced(self[values].values,
    414                                                index=index)
--> 415     return indexed.unstack(columns)
    416 
    417 

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\frame.py in unstack(self, level, fill_value)
   5532         """
   5533         from pandas.core.reshape.reshape import unstack
-> 5534         return unstack(self, level, fill_value)
   5535 
   5536     _shared_docs['melt'] = ("""

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\reshape\reshape.py in unstack(obj, level, fill_value)
    493     if isinstance(obj, DataFrame):
    494         if isinstance(obj.index, MultiIndex):
--> 495             return _unstack_frame(obj, level, fill_value=fill_value)
    496         else:
    497             return obj.T.stack(dropna=False)

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\reshape\reshape.py in _unstack_frame(obj, level, fill_value)
    507         unstacker = partial(_Unstacker, index=obj.index,
    508                             level=level, fill_value=fill_value)
--> 509         blocks = obj._data.unstack(unstacker)
    510         return obj._constructor(blocks)
    511     else:

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\internals.py in unstack(self, unstacker_func)
   4608         unstacked : BlockManager
   4609         """
-> 4610         dummy = unstacker_func(np.empty((0, 0)), value_columns=self.items)
   4611         new_columns = dummy.get_new_columns()
   4612         new_index = dummy.get_new_index()

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\reshape\reshape.py in __init__(self, values, index, level, value_columns, fill_value, constructor)
    135 
    136         self._make_sorted_values_labels()
--> 137         self._make_selectors()
    138 
    139     def _make_sorted_values_labels(self):

c:\users\apex_predator\appdata\local\programs\python\python36\lib\site-packages\pandas\core\reshape\reshape.py in _make_selectors(self)
    173 
    174         if mask.sum() < len(self.index):
--> 175             raise ValueError('Index contains duplicate entries, '
    176                              'cannot reshape')
    177 

ValueError: Index contains duplicate entries, cannot reshape

我想念的是什么?

1 个答案:

答案 0 :(得分:0)

似乎您在DataFrame中有重复的行,因此您的Pivot不知道旋转时要取哪一行。

尝试使用此duplicated方法进行检查。