如何在python中调整Dataframe的大小以包含源/PDF中的第一行



请帮忙。

Python Dataframe从缺少/不包含第一行的pdf中读取/返回如下数据。这可能是因为pdf是如何在源代码处生成的。数据帧中的样本数据图像是否有一种方法来调整或重组,以选择表的第一行?请帮助。

import tabula
import pandas as pd
file = "sample.pdf"
tables = tabula.read_pdf(file, pages=1, multiple_tables=True)
df = pd.DataFrame(tables[0])
df = df.reset_index()
for index, row in df.iterrows():
    print(row[0], row[1], row[2], row[3],row[4])

正如我所看到的输出图像,tabula已将数据的第一行视为表头。这可能是因为没有出现标题,所以Tabula将第一行视为列名。

阻止Tabula将第一行转换为列头的最简单方法是使用Tabula的pandas_options参数。

添加如下参数:

tables = tabula.read_pdf(file, pages=1, multiple_tables=True, pandas_options={'header':None})

这将阻止Tabula将第一行数据转换为列标题。

最新更新