使用 python 从电子表格中提取多个表

Nab*_*bla 5 python excel pandas

我想提取一系列 Excel 电子表格的多个表,其中某些工作表可能包含多个表,以将表单独存储为例如 csv 文件。该表可能是这样的:

在此输入图像描述

如果我使用 pandas read_excel 读取它

import pandas as pd
pd.read_excel('table_example.xlsx',header=None)
Run Code Online (Sandbox Code Playgroud)

我会得到这样的东西:

在此输入图像描述

我怎样才能提取不同的表?就我而言,表具有 NaN 值,这可能会带来额外的复杂性。

[EDIT1] 可以使用 pandas 生成类似于 Excel 表格的内容:

df=pd.DataFrame(np.nan,index=range(0,10),columns=range(0,10))
df.iloc[1,2:5]=['t1h1','t1h2','t1h3']
df.iloc[2:5,2:5]=np.random.randn(3,3)
df.iloc[6,3:7]=['t2h1','t2h2','t2h3','t2h4']
df.iloc[7:9,3:7]=np.random.randn(2,4)
Run Code Online (Sandbox Code Playgroud)

我尝试使用内置的 pandas 函数找到表的限制:

df[df.isnull().all(axis=1)]
Run Code Online (Sandbox Code Playgroud)

我可以使用第一行和第二行设置水平划分,也许可以进行第一次分割,但我不知道如何选择已识别行上方或下方的单元格。或者即使这是最方便的方法。

免责声明:在我的例子中,表格在标题上方的行中总是有一个标签,这是因为这些表格是由非 python 软件读取的,该软件使用它们来识别表格的开始位置。我决定不考虑这些标签来提出其他人可能遇到的更通用的问题。

小智 5

只要 2 个表由一行或一列 NaN 分隔,这可能有助于动态定位和提取表。

我使用了/sf/answers/3827286851/中的边界框解决方案

from skimage.measure import label, regionprops
Run Code Online (Sandbox Code Playgroud)
#this basically converts your table into 0s and 1s where 0 is NaN and 1 for non NaN 
binary_rep = np.array(df.notnull().astype('int'))

list_of_dataframes = []
l = label(binary_rep)
for s in regionprops(l):
    #the bbox contains the extremes of the bounding box. So the top left and bottom right cell locations of the table.
    list_of_dataframes.append(df.iloc[s.bbox[0]:s.bbox[2],s.bbox[1]:s.bbox[3]])

Run Code Online (Sandbox Code Playgroud)


gxp*_*xpr 4

import numpy as np
import pandas as pd

# I have assumed that the tables are "separated" by at least one row with only NaN values

df=pd.DataFrame(np.nan,index=range(0,10),columns=range(0,10))
df.iloc[1,2:5]=['t1h1','t1h2','t1h3']
df.iloc[2:5,2:5]=np.random.randn(3,3)
df.iloc[6,3:7]=['t2h1','t2h2','t2h3','t2h4']
df.iloc[7:9,3:7]=np.random.randn(2,4)

print(df)

# Extract by rows

nul_rows = list(df[df.isnull().all(axis=1)].index)

list_of_dataframes = []
for i in range(len(nul_rows) - 1):
    list_of_dataframes.append(df.iloc[nul_rows[i]+1:nul_rows[i+1],:])


# Remove null columns

cleaned_tables = []
for _df in list_of_dataframes:
    cleaned_tables.append(_df.dropna(axis=1, how='all'))

# cleaned_tables is a list of the dataframes

print(cleaned_tables[0])
print(cleaned_tables[1])
Run Code Online (Sandbox Code Playgroud)