我使用表格阅读表格格式pdf文件。
tables = tabula.read_pdf(file, pages="all")
这个很好用。现在,表是一个数据文件列表,其中每个数据帧都是一个来自pdf文件的表。
然而,表行被编入索引0、1、2、3。等等,但是第一行作为每个数据文件的列名或标题。
当前数据:
Component manufacturer DMNS
0 Component name KL32/OOH8
1 Component type LTE-M/NB-IoT
2 Package markings <pin 1 marker>\ ksdc 99cdjh
3 Date code Not discerned
4 Package type 127-pin land grid array (LGA)
5 Package size 26.00 mm × 10.11 mm × 3.05 mm
期望的Dataframe:
0 1
0 Component manufacturer DMNS
1 Component name KL32/OOH8
2 Component type LTE-M/NB-IoT
3 Package markings <pin 1 marker>\ ksdc e99cdjh
4 Date code Not discerned
5 Package type 127-pin land grid array (LGA)
6 Package size 26.00 mm × 10.11 mm × 3.05 mm
我该怎么做这个转变呢?
发布于 2022-07-24 17:59:38
作为tabula docs on read_pdf
状态,您可以添加pandas_options
,它们甚至给出了您需要的示例-- {'header': None}
。所以(类似的)这应该能起作用:
tabula.read_pdf(file, pages="all", pandas_options={'header': None})
编辑:所以很明显,只有当您将multiple_tables
设置为False
时,它才会起作用,而这不是默认的。我会稍微考虑一下这些选项,如果它没有给出想要的结果,here是一篇关于如何将列名转换为第一行的文章。
发布于 2022-07-24 18:06:31
这里有一个方法来做你的问题所要求的:
df = df.T.reset_index().T.reset_index(drop=True)
输出:
0 1
0 Component manufacturer DMNS
1 Component name KL32/OOH8
2 Component type LTE-M/NB-IoT
3 Package markings <pin 1 marker>\ ksdc 99cdjh
4 Date code Not discerned
5 Package type 127-pin land grid array (LGA)
6 Package size 26.00 mm × 10.11 mm × 3.05 mm
解释:
将数据转换为column
reset_index()
将索引(即原始列标签)再次转换为新的初始列,从而使新的初始列成为初始行,并使用reset_index()
获得新的整数索引。https://stackoverflow.com/questions/73100770
复制相似问题