【问题标题】:Rearranging Table after PDF extraction with Tabula使用 Tabula 提取 PDF 后重新排列表格
【发布时间】:2017-12-04 21:06:38
【问题描述】:

我使用 Tabula 从 PDF 中提取表格。它的工作将减去一点清理。我遇到的最后一个问题,我不确定如何解决,如果单元格行太大(即它包含包装的文本),那么 Tabula 将它分成两行,其中一行包含几乎所有信息,第二行包含上一行中第一个单元格文本的后半部分,但其余单元格中为“无”。

这是一个例子:

    df

       House_Type             Area        Shape

       Blue House       3456          circle
       Red house        2345          square
       Small Green      987           square
       House            None          None

理想情况下,我可以将“House_Type”行“house”中的文本添加到“House_Type”列上方的行中,然后删除其中包含“none”的“house”行。

最终结果如下所示:

      df

       House_Type             Area        Shape

       Blue House             3456          circle
       Red house              2345          square
       Small Green House      987           square

我认为将其作为循环可能会起作用

    def row_funct( df):
        row = 0
        for row in df:
            if area = None:
            row += 1

但我不知道从这里往哪里走,或者这是否是正确的方向

【问题讨论】:

    标签: python pandas loops pdf tabula


    【解决方案1】:

    我遇到了类似的问题并编写了以下函数(稍作修改以匹配您的示例):

    def CleanRunResults(df):
        for row in range(len(df)-1, -1, -1):
            NoArea = pd.isnull(df['Area'].iloc[row])
            NoShape = pd.isnull(df['Shape'].iloc[row])
            YesType = pd.notnull(df['House_Type'].iloc[row])
            PrevRow = row - 1
            if NoArea and NoShape and YesType:
                df['House_Type'].iloc[PrevRow] = '{0}{1}'.format(df['House_Type'].iloc[PrevRow],df['House_Type'].iloc[row] )
        df.dropna(subset=['Shape', 'Area'], how='all', inplace=True)
        df = df[['House_Type', 'Shape', 'Area']]
        return(df)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多