1. <em id="yud1w"><acronym id="yud1w"><u id="yud1w"></u></acronym></em>
      
      
      <button id="yud1w"></button>

      python

      当前位置:首页?>?疑难杂症?>?当前文章

      疑难杂症

      InvalidIndexError: Reindexing only valid with uniquely valued Index objects

      2021-08-20 52赞 老董笔记
      每篇文章努力于解决一个问题!更多精品资源可移步文章底部。

        在用pandas做数据拼接的时候可能遇到过如下报错:InvalidIndexError: Reindexing only valid with uniquely valued Index objects。

        这个问题我也遇见了,是在使用concat函数让2个df数据横向拼接时遇到的。看报错的意思就是拼接前单个df的索引值不可以有重复。经过测试确实如此。

        代码如下(df1和df2相同,横向拼接;df3的index索引特意设为重复值拼接时报错),自行体会。(在用其他函数时可能也会遇到同样的报错,报错原因是一致的)

      # -*- coding:UTF-8 -*-
      import pandas as pd
      
      df1 = pd.DataFrame([['python66.com', 'dong'], ['baidu.com','li']],
                         columns=['site', 'author'])
      
      df2 = pd.DataFrame([['python66.com', 'dong'], ['baidu.com','li']],
                         columns=['site', 'author'])
      
      df = pd.concat([df1,df2],axis=1)
      print(df)
      print('--------------------')
      
      df3 = pd.DataFrame([['python66.com', 'dong'], ['baidu.com','li']],
                         columns=['site', 'author'],index=[0,0])
      df = pd.concat([df1,df3],axis=1)
      print(df)
      
      
                 site author          site author
      0  python66.com   dong  python66.com   dong
      1     baidu.com     li     baidu.com     li
      --------------------
      Traceback (most recent call last):
        File "D:/py3script/pandas_xuexi/test.py", line 16, in <module>
          df = pd.concat([df1,df3],axis=1)
        File "D:installpyinstalllibsite-packagespandascore
      eshapeconcat.py", line 298, in concat
          return op.get_result()
        File "D:installpyinstalllibsite-packagespandascore
      eshapeconcat.py", line 516, in get_result
          indexers[ax] = obj_labels.get_indexer(new_labels)
        File "D:installpyinstalllibsite-packagespandascoreindexesase.py", line 3171, in get_indexer
          raise InvalidIndexError(
      pandas.errors.InvalidIndexError: Reindexing only valid with uniquely valued Index objects
      
      
      

      文章评论

      InvalidIndexError: Reindexing only valid with uniquely valued Index objects文章写得不错,值得赞赏
      国产99视频精品免视看6