pandas :从下面的行开始读取具有特定值的Excel文件 [英] Pandas: reading Excel file starting from the row below that with a specific value
问题描述
说我有以下Excel文件:
Say I have the following Excel file:
A B C
0 - - -
1 Start - -
2 3 2 4
3 7 8 4
4 11 2 17
我想读取数据框中的文件,以确保我开始在
I want to read the file in a dataframe making sure that I start to read it below the row where the Start
value is.
注意:Start
值并不总是位于同一行中,因此如果我要使用:
Attention: the Start
value is not always located in the same row, so if I were to use:
import pandas as pd
xls = pd.ExcelFile('C:\Users\MyFolder\MyFile.xlsx')
df = xls.parse('Sheet1', skiprows=4, index_col=None)
这将失败,因为需要修复skiprows
.是否有任何解决方法可确保xls.parse
找到字符串值而不是行号?
this would fail as skiprows
needs to be fixed. Is there any workaround to make sure that xls.parse
finds the string value instead of the row number?
推荐答案
df = pd.read_excel('your/path/filename')
此答案有助于查找在df中开始
This answer helps in finding the location of 'start' in the df
for row in range(df.shape[0]):
for col in range(df.shape[1]):
if df.iat[row,col] == 'start':
row_start = row
break
在具有row_start之后,您可以使用熊猫的子帧
after having row_start you can use subframe of pandas
df_required = df.loc[row_start:]
如果您不需要包含开始"的行,只需将row_start加1
And if you don't need the row containing 'start', just u increment row_start by 1
df_required = df.loc[row_start+1:]
这篇关于 pandas :从下面的行开始读取具有特定值的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!