user3447653
user3447653

Reputation: 4148

Loop through two pandas dataframes

I have two dataframes df1 and df2 as shown below:

df1:

Month    Count
6        314
6        418
6        123
7        432

df2:

Month  ExpectedValue
6       324
7       512
8       333

I have to loop through df1 and df2. If df1['Month'] == 6, then I have to loop through df2 to get the expected value for month 6. Then, I will have the field in df1 as df1['ExpectedValue'].

Output like this below:

df1:

Month   Count    ExpectedValue
6        314        324 
6        418        324
6        123        324
7        432        512

Is looping through 2 dataframes an efficient idea? Any help would be appreciated.

Upvotes: 2

Views: 2399

Answers (1)

root
root

Reputation: 33793

In general, you shouldn't loop over DataFrames unless it's absolutely necessary. You'll usually get better performance using a built-in Pandas function that's already been optimized, or by using a vectorized approach. This will usually result in cleaner code too.

In this case you can use DataFrame.merge:

df1 = df1.merge(df2, how='left', on='Month')

The resulting output:

   Month  Count  ExpectedValue
0      6    314            324
1      6    418            324
2      6    123            324
3      7    432            512

Upvotes: 7

Related Questions