附加从循环生成的表

Appending tables generated from a loop

我是这里的新 python 用户,我正在尝试将我使用 Camelot 从 pdf 中提取的数据附加在一起,但无法将它们连接在一起。

这是我的代码:

url = 'https://www.fhfa.gov/DataTools/Downloads/Documents/HPI/HPI_AT_Tables.pdf'

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

i = 0

while i in range(0,tables.n):
    header = tables[i].df.index[tables[i].df.iloc[:,0]=='Metropolitan Statistical Area'].to_list()
    header = str(header)[1:-1]
    header = (int(header))
    tables[i].df = tables[i].df.rename(columns = tables[i].df.iloc[header])
    tables[i].df = tables[i].df.drop(columns = {'': 'Blank'})

    print(tables[i].df)
    #appended_data.append(tables[i].df)
    
#if i > 0:
#    dfs = tables[i-1].append(tables[i], ignore_index = True)
#pass

    i = i + 1

任何帮助将不胜感激

您可以使用 pandas.concat() 连接数据帧列表。

while i in range(0,tables.n):
    header = tables[i].df.index[tables[i].df.iloc[:,0]=='Metropolitan Statistical Area'].to_list()
    header = str(header)[1:-1]
    header = (int(header))
    tables[i].df = tables[i].df.rename(columns = tables[i].df.iloc[header])
    tables[i].df = tables[i].df.drop(columns = {'': 'Blank'})

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