Pra*_*nka 3 python excel python-2.7 pandas
我在 excel 中有很多行,这些行在空行之后填充了垃圾值。有没有办法使用 Python pandas 仅读取 excel 中第一个空行之前的记录。
我不知道 read_excel 是否可以做到这一点。如果您从 excel 导入空行,这些行的列值将填充为 NaN,然后您可以选择这些值,直到第一行填充所有 NaN。
我假设你的数据是这样的,你有一个空行,后面的数据是垃圾(我包含了多个空行和垃圾)
df = pd.read_excel(r'Book1.xlsx') # read the file
print df
'''
col1 col2 col3
0 1 2 3
1 1 2 3
2 1 2 3
3 1 2 3
....
10 1 2 3
11 NaN NaN NaN
12 x x x
....
18 NaN NaN NaN
19 NaN NaN NaN
20 y y y
21 y y y
....
'''
first_row_with_all_NaN = df[df.isnull().all(axis=1) == True].index.tolist()[0]
# gives me the first row number of the row that has all the values to be NaN.
'''
11
'''
print df.loc[0:first_row_with_all_NaN-1]
# then I use loc to select the rows from 0 to first row with all NaN's-1
'''
col1 col2 col3
0 1 2 3
1 1 2 3
2 1 2 3
3 1 2 3
4 1 2 3
5 1 2 3
6 1 2 3
7 1 2 3
8 1 2 3
9 1 2 3
10 1 2 3
'''
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3895 次 |
最近记录: |