简体   繁体   English

如何使用 python 中的 iterrows 从特定行读取 csv 文件?

[英]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.我是 python 的新手,我想知道如何从文件中的特定行读取 csv 文件。 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.实际上我从这里找到了一个先前的问题: Pandas 从指定的行号迭代行,但似乎这不完全是我的情况,因为我没有具体的开始日期,但从索引开始应该很好。

For my case I have a csv file containing 1000 rows as the following code below:对于我的情况,我有一个包含 1000 行的 csv 文件,如下代码所示:

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?我想也许有一种方法可以用file_read设置条件,但我不知道该怎么做我想从第 400 行开始,所以for循环中的打印行应该打印 400。有什么办法可以做到这一点? Thank you so much for your Help.非常感谢你的帮助。

To address rows by index, simply use the handy iloc function and slicing:要按索引处理行,只需使用方便的iloc function 和切片:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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