微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从循环连接表得到错误 - InvalidIndexError:重新索引仅对唯一值的索引对象有效

如何解决从循环连接表得到错误 - InvalidIndexError:重新索引仅对唯一值的索引对象有效

我需要连接从循环创建的表。列中有重复的名称,但它们讲述的是不同的故事,但由于某种原因,在运行此代码时出现错误

InvalidindexError: Reindexing only valid with uniquely valued Index objects

代码如下:

url = 'https://www.impactfees.com/publications%20pdf/2019survey.pdf'

tables = camelot.read_pdf(url,flavor = 'stream',edge_tol = 500,pages = '4-end')

i = 0


while i in range(0,tables.n):
    table_value = tables[i].df.loc[0,4]
    header = 1
    header = tables[i].df.iloc[header]
    tables[i].df = tables[i].df.rename(columns = header)
    
    nan_v = float("NaN")
    tables[i].df.replace('',nan_v,inplace = True) 
    tables[i].df.dropna(subset = ['Jurisdiction'],inplace = True)
    tables[i].df.replace(['Jurisdiction'],inplace = True)
    tables[i].df.dropna(subset = ['Jurisdiction'],inplace = True)

#    Tot_col = tables[i].df.columns.get_loc('Total')
#    tables[i].df = tables[i].df.iloc[:,0:Tot_col+1]
    tables[i].df['report_name'] = table_value
    tables[i].df.loc[~tables[i].df.index.duplicated(keep = 'first')]
    i = i + 1

dfs = pd.concat([table.df for table in tables])

dfs

这是我得到的错误

InvalidindexError                         Traceback (most recent call last)
<ipython-input-133-2617eb5ae448> in <module>
     23     i = i + 1
     24 
---> 25 dfs = pd.concat([table.df for table in tables])
     26 
     27 

~\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in concat(objs,axis,join,ignore_index,keys,levels,names,verify_integrity,sort,copy)
    296     )
    297 
--> 298     return op.get_result()
    299 
    300 

~\anaconda3\lib\site-packages\pandas\core\reshape\concat.py in get_result(self)
    514                     obj_labels = obj.axes[1 - ax]
    515                     if not new_labels.equals(obj_labels):
--> 516                         indexers[ax] = obj_labels.get_indexer(new_labels)
    517 
    518                 mgrs_indexers.append((obj._mgr,indexers))

~\anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_indexer(self,target,method,limit,tolerance)
   3169 
   3170         if not self.is_unique:
-> 3171             raise InvalidindexError(
   3172                 "Reindexing only valid with uniquely valued Index objects"
   3173             )

InvalidindexError: Reindexing only valid with uniquely valued Index objects

解决方法

  • camelot 有问题。我不得不修补 utils.py 以使用不同的 user-agent
  • 页面不完全一致,因此将 list 传递给 rename(columns=) 不起作用。你需要传递一个dict
  • 保留了两个数据框 - 一个包含目标行,另一个包含排除的行
  • 仍然存在不一致的列,例如排水公园
import pandas as pd
import camelot

url = 'https://www.impactfees.com/publications%20pdf/2019survey.pdf'
tables = camelot.read_pdf(url,flavor = 'stream',edge_tol = 500,pages = '4-end')

df = pd.DataFrame()
dfexc = pd.DataFrame()
for i in range(tables.n):
    dft = tables[i].df.rename(columns={i:v.replace("\n"," ") for i,v in tables[i].df.iloc[1].items() if v!=""})
    if " " in dft.columns[0]: 
        dft = dft.rename(columns={dft.columns[0]:dft.columns[0].split(" ")[0],1:dft.columns[0].split(" ")[1]})
    m = (dft.State.str.len()!=2) | (dft.index < 2)

    dfexc = pd.concat([dfexc,tables[i].df.loc[m].assign(page=i)])
    df = pd.concat([df,dft.loc[~m].assign(page=i)])#.reset_index(drop=True)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?