2017-04-12 64 views
2

是否有可能使用pandas从纸张excel文件中读取多个表? 是这样的:从ROW0 读表1直到从102行row100 读取表2直到row202 ...pandas read_excel在同一张纸上的多个表

+1

为什么不读这一切,然后分开, python中不同的DataFrame? – splinter

+0

我不知道我该怎么做到这一点。 – bsd

+0

@bsd,你知道预先的总行数吗? – MaxU

回答

5

假设我们有以下的Excel文件:

enter image description here

解决方案:我们解析第一片(索引:0

xl = pd.ExcelFile(fn) 
nrows = xl.book.sheet_by_index(0).nrows 

df1 = xl.parse(0, skip_footer = nrows-(10+1)).dropna(axis=1, how='all') 
df2 = xl.parse(0, skiprows=12).dropna(axis=1, how='all') 

结果:

In [123]: df1 
Out[123]: 
    a b c 
0 78 68 33 
1 62 26 30 
2 99 35 13 
3 73 97 4 
4 85 7 53 
5 80 20 95 
6 40 52 96 
7 36 23 76 
8 96 73 37 
9 39 35 24 

In [124]: df2 
Out[124]: 
    c1 c2 c3 c4 
0 78 88 59 a 
1 82 4 64 a 
2 35 9 78 b 
3 0 11 23 b 
4 61 53 29 b 
5 51 36 72 c 
6 59 36 45 c 
7 7 64 8 c 
8 1 83 46 d 
9 30 47 84 d 
1

在整个csv文件第一读:

import pandas as pd 
df = pd.read_csv('path_to\\your_data.csv') 

,然后获得各个帧,为例如,使用:

df1 = df.iloc[:100,:] 
df2 = df.iloc[100:200,:] 
+1

如果它是一个CSV文件,我们可以简单地使用'skiprows'和'nrows'参数。不幸的是'nrows'没有为'pd.read_excel'实现 – MaxU

相关问题