【问题标题】:How to loop in tabula-py data format in python如何在 python 中循环 tabula-py 数据格式
【发布时间】:2021-11-27 15:31:56
【问题描述】:

我想知道如何在 python 中从 pdf 文件中提取特定的表列。

到目前为止我的代码

    import tabula.io as tb
from tabula.io import read_pdf
dfs = tb.read_pdf(pdf_path, pages='all')
print (len(dfs)) [It displays 73]

我可以通过 print (dfs[2]['Section ID']) 访问单个表格列 我想知道如何使用 for 循环搜索所有数据框中的特定列。

我想做这样的事情

for i in range(len(dfs)):
    if (dfs[i][2]) == 'Section ID ' //(This gives invalid syntax)
    print dfs[i]

【问题讨论】:

    标签: python pandas dataframe tabula-py


    【解决方案1】:

    如果您只有一个名称为 Section ID 的数据框(或仅对此列的第一个数据框感兴趣),您可以遍历 read_pdf 返回的列表,使用 in df.columns 和 @ 检查列是否存在找到匹配项时为 987654324@。

    import tabula.io as tb
    from tabula.io import read_pdf
    df_list = tb.read_pdf(pdf_path, pages='all')
    
    for df in df_list:
        if 'Section ID' in df.columns:
            break
    print(df)
    

    如果您可能有多个带有 Section ID 列的数据框,您可以使用列表解析过滤器并获取具有该列名称的数据框的列表

    dfs_section_id = [df for df in df_list if 'Section ID' in df.columns]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      • 1970-01-01
      • 2018-04-28
      相关资源
      最近更新 更多