InvalidIndexError: Reindexing only valid with uniquely valued Index objects

刘超 13天前 ⋅ 114 阅读   编辑

一、描述

  pandas 执行concat操作,报如下错误

Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 810, in __bootstrap_inner
    self.run()
  File "/usr/lib/python2.7/threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/sdev/liujichao/pull_dsp_report.py", line 149, in wrapper
    return func(insertTiDB=insertTiDB,args=args,dsp=dsp,responses=responses)
  File "./dsp_report/HybridAI.py", line 34, in HybridAI
    resultDF = fillDF(args,dsp,resultDF)
  File "/home/sdev/liujichao/pull_dsp_report.py", line 252, in fillDF
    resultDF["day"] = resultDF["day"].astype(int)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2686, in __getitem__
    return self._getitem_multilevel(key)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 2735, in _getitem_multilevel
    result = self.reindex(columns=new_columns)
  File "/usr/local/lib/python2.7/dist-packages/pandas/util/_decorators.py", line 187, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 3566, in reindex
    return super(DataFrame, self).reindex(**kwargs)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/generic.py", line 3689, in reindex
    fill_value, copy).__finalize__(self)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 3496, in _reindex_axes
    fill_value, limit, tolerance)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/frame.py", line 3518, in _reindex_columns
    tolerance=tolerance)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/multi.py", line 2066, in reindex
    tolerance=tolerance)
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/multi.py", line 2015, in get_indexer
    indexer = self._engine.get_indexer(target)
  File "pandas/_libs/index.pyx", line 654, in pandas._libs.index.BaseMultiIndexCodesEngine.get_indexer
  File "pandas/_libs/index.pyx", line 648, in pandas._libs.index.BaseMultiIndexCodesEngine._extract_level_codes
  File "/usr/local/lib/python2.7/dist-packages/pandas/core/indexes/base.py", line 3244, in get_indexer
    raise InvalidIndexError('Reindexing only valid with uniquely'
InvalidIndexError: Reindexing only valid with uniquely valued Index objects

二、解决方法

  待合并的两个dataFrame索引并不相同,需要对他们分别重新设置索引

A = A.reset_index()
B = B.reset_index()

  然后再应用concat函数,就可以正常合并了


注意:本文归作者所有,未经作者允许,不得转载

全部评论: 0

    我有话说: