0

I am trying to join two dataframes, one has Id's and phone numbers, another one has bunch of other columns along with same Id field (however there are some duplicate Id's in this DF). How can I join the phone number column from first dataframe to the second one? I tried doing this but I am getting duplicated key error:

df= df.join(other.set_index('Id'), on='Id', how='outer')

How can I accomplish this? (I want duplicate ID's in second DF to have same phone numbers as non-duplicate ones)

Ach113
  • 1,775
  • 3
  • 18
  • 40
  • Use `merge` instead of `join`, since you cannot have duplicates in your index, thus `set_index('Id')`, will throw an error. – Erfan Oct 25 '19 at 10:11
  • @Erfan, I see. Could you write down approximate syntax? I tried join earlier but it didnt work for some reason, I think I might have written the code for merge incorrectly. – Ach113 Oct 25 '19 at 10:12
  • [Here's](https://stackoverflow.com/questions/53645882/pandas-merging-101) an extensive read on how to merge in pandas – Erfan Oct 25 '19 at 11:20

1 Answers1

0

Try the following with pandas.merge method, you can read the documentation here.

df= df.merge(other.set_index('Id'), suffixes = ('_l', '_r'), how='outer')

This will merge two dataframes and add suffixes for exact matching column names.

null
  • 1,944
  • 1
  • 14
  • 24