This question already has answers here:
Pandas pivot table ValueError: Index contains duplicate entries, cannot reshape
                                
                                    (2个答案)
                                
                        
                                2年前关闭。
            
                    
我想在清洁和预处理后在给定的最终表中的标签和用户名之间绘制热图。
出现以下错误。

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

python - 枢轴:ValueError:索引包含重复项,无法重塑[duplicate]-LMLPHP

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


我想念的是什么?

最佳答案

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

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

关于python - 枢轴:ValueError:索引包含重复项,无法重塑[duplicate],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52665214/

10-12 18:41