lakshmen
lakshmen

Reputation: 29064

Looping through two data frames in python

I would like to loop through two data frames at the same time

x is where i need the interest data to go in. How do i get the interest data for the variable x?

Upvotes: 3

Views: 4698

Answers (3)

Minions
Minions

Reputation: 5477

What about using zip:

for (i, row1), (j, row2) in zip(df1.iterrows(), df2.iterrows()):
    print(row1)
    print(row2)

Upvotes: 2

jezrael
jezrael

Reputation: 862611

There are same index and columns values, so is possible select second DataFrame by index and columns values from first one by Series.at or Series.loc:

for r in usd_margin_data.index:
    for c in usd_margin_data.columns:
        print (usd_margin_data.at[r, c])
        print (interest_data.at[r, c])  

Upvotes: 3

Mohamed Thasin ah
Mohamed Thasin ah

Reputation: 11192

first merge both df, then apply loop.

df=pd.merge(usd_margin_data,interest_data,on=['acct'],suffixes=['_margin','_interest'])

Now you can loop each row access your both df rows from iterrows().

Note: value from both df differs from it's name Suffix

Upvotes: 1

Related Questions