Louis
Louis

Reputation: 25

Add column from other dataframe on a specific column while keeping original indexes

New to Pandas and trying to add a column from df2 to df1 based on a column that both of the dataframes have in common. I want to preserve the indexes in df1. Notice that df2 has lots of duplicate rows, including duplicate ItemIds.

DataFrame 1

     ItemId
0    1
1    4
2    7
3    8
DataFrame 2

0    column1    ItemId    column_to_merge
1    21984      1         apple
2    89767      1         apple
3    84595      2         pear
4    90876      4         tree
5    59876      5         cookie
6    50758      5         cookie
7    85738      7         monster
8    34980      8         kick



Expected outcome

DataFrame 1

     ItemId    column_to_merge
0    1         apple
1    4         tree
2    7         monster
3    8         kick

I want to preserve the indexes of DataFrame 1 and add the column of DataFrame 2 to DataFrame 1.

Any help is much appreciated.

I dropped duplicates in df2 before doing the merge as proposed by Karthik Katragadda, so that the merge can be done without including the duplicates.

Upvotes: 0

Views: 38

Answers (1)

Karthik Katragadda
Karthik Katragadda

Reputation: 176

A simple left merge will do the job for you.

Try this :

dataframe_1 = dataframe_1.merge(dataframe_2[['ItemId','column_to_merge']], on = 'ItemId', how = 'left')

Upvotes: 1

Related Questions