【问题标题】:Pivot: ValueError: Index contains duplicate entries, cannot reshape [duplicate]枢轴:ValueError:索引包含重复条目,无法重塑 [重复]
【发布时间】:2018-10-05 12:01:50
【问题描述】:

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

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

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

我错过了什么?

【问题讨论】:

  • 我删除了 seaborn 标签并编辑了标题,因为您的错误与 pandas.pivot 相关,而不是 seaborn

标签: python pandas pivot-table


【解决方案1】:

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

试试这个duplicated 方法来检查它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 2018-05-31
    • 1970-01-01
    • 2018-02-09
    • 1970-01-01
    • 2022-01-24
    • 2021-12-22
    相关资源
    最近更新 更多