How to change the starting index of iterrows()?

Question:

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)?

Asked By: SaikiHanee

||

Answers:

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
Answered By: mechanical_meat

Try using itertools.islice

from itertools import islice

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

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

for i, r in df.iloc[1:].iterrows():
Answered By: acushner
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.