Home » Python » iterating row by row through a pandas dataframe [duplicate]

iterating row by row through a pandas dataframe [duplicate]

Posted by: admin November 1, 2017 Leave a comment

Questions:

Possible Duplicate:
What is the most efficient way to loop through dataframes with pandas?

I’m looking to iterate row by row through a pandas DataFrame. The way I’m doing it so far is as follows:

for i in df.index:
    do_something(df.ix[i])

Is there a more performant and/or more idiomatic way to do this? I know about apply, but sometimes it’s more convenient to use a for loop. Thanks in advance.

Answers:

You should use df.iterrows(). Though iterating row-by-row is not especially efficient since Series objects have to be created.