pandas 如何更改 iterrows() 的起始索引?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38596056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 01:40:26  来源:igfitidea点击:

How to change the starting index of iterrows()?

pythonpandasdataframe

提问by SaikiHanee

We can use the following to iterate rows of a data frame.

我们可以使用以下内容来迭代数据框的行。

for index, row in df.iterrows():

What if I want to begin from a different row index? (not from first row)?

如果我想从不同的行索引开始怎么办?(不是第一行)?

回答by acushner

i know this has an answer, but why not just do:

我知道这有一个答案,但为什么不这样做:

for i, r in df.iloc[1:].iterrows():

回答by imreal

Try using itertools.islice

尝试使用 itertools.islice

from itertools import islice

for index, row in islice(df.iterrows(), 1, None):

回答by mechanical_meat

Sure:

当然:

for i,(index,row) in enumerate(df.iterrows()):
    if i == 0: continue # skip first row

Or something like:

或者类似的东西:

for i,(index,row) in enumerate(df.iterrows()):
    if i < 5: continue # skip first 5 rows