简体   繁体   中英

How to read csv file from a specific row using iterrows in python?

I am new to python and I wonder how to read the csv file from a specific row in the file. Actually I found a previous question from here: Pandas Iterate through rows from specified row number but it seems It is not exactly my case as I dont have specific date to start but it should be good to start with index.

For my case I have a csv file containing 1000 rows as the following code below:

import pandas as pd
file_read = pd.read_csv('1000_rows.csv')

for line,row in file_read.iterrows():
    print(line)

I think perhaps there is a way to set the condition with file_read but I dont know how to do it I want to start from line 400th so the print line in the for loop should print 400. Is there any way to do this? Thank you so much for your Help.

To address rows by index, simply use the handy iloc function and slicing:

import pandas as pd
file_read = pd.read_csv('1000_rows.csv')

for line,row in file_read.iloc[400:].iterrows():
    print(line)

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