简体   繁体   中英

Python Pandas: Skip rows by particular pattern (not row number) using pd.read_csv

I'm trying to import the csv file into the Pandas DataFrame. However, here is the challenge, for example, I cannot use skiprows=9 , because the csv format is inconsistent from time to time, it will contain some useless information before the actual table begins.

Fortunately, before the table starts, there will always have a single row with the string "report field", and then the real table starts from the next line.

Is there any way I can skip all the rows until it catches the pattern "report field"?

Thanks.

df= pandas.read_csv("file.csv",header= None)
df_2= df.iloc[(df.loc[df[0]=='report field'].index[0]+1):, :].reset_index(drop = True)

So, the above line searches for "report field" value in "0" column of "df" dataframe and then picks up data from the next row to the last row in the "file.csv" file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM