deepak dash
deepak dash

Reputation: 245

How to iterate over multiple dataframe rows at the same time in pandas

How to iterate over multiple dataframe rows at the same time in pandas df1,df2

for index1 row1 in df1.iterrows():
 for index2, row2 in df2.iterrows():

i want to use one for loop instead of two for loop.

Upvotes: 0

Views: 849

Answers (1)

Mustafa Aydın
Mustafa Aydın

Reputation: 18306

There is zip for that

for (index_1, row_1), (index_2, row_2) in zip(df1.iterrows(), df2.iterrows()):
    ...

Although there is a very high chance you don't need this loop at all but instead use pandas-native operations to deal with the frames.

Upvotes: 3

Related Questions