ループから作成されたテーブルを連結する必要があります。列に名前の繰り返しがありますが、それらは別の話をしていますが、何らかの理由でこのコードを実行するとエラーが発生します:
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'], nan_v, 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