1

I have a dataframe called REF with the following structure:

old_id  new_id
     3       6
     4       7
     5       8

I want to replace all the values that can be found equal to any of the old_id values in another dataframe NEW that is:

old_id  column_1  column_2
     3         a         e
     4         b         f
     9         c         g
     9         d         h

Therefore the new output dataset NEW will be:

old_id  column_1  column_2
     6         a         e
     7         b         f
     9         c         g
     9         d         h
manosbar
  • 318
  • 2
  • 3
  • 15

1 Answers1

3

Use map:

s = df1.set_index('old_id')['new_id']

df2['old_id'] = df2['old_id'].map(s).fillna(df2['old_id'])

Or slowier solution with replace:

df2['old_id'] = df2['old_id'].replace(s)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252