0

I have the following df1:

 Id   value
'so'   5
'fe'   6
'd1'   4 

Then I have a ref_df:

 Id    value
'so'    3
'fe'    3
'ju'    2
'd1'    1

I want to check that if any of the Ids in ref_df appear in df1, then replace the value in df1 by the ref_df.

The desired output would be:

 Id   value
'so'   3
'fe'   3
'd1'   1

How can I achieve this?

Malik Asad
  • 441
  • 4
  • 15
JamesHudson81
  • 2,215
  • 4
  • 23
  • 42
  • @jezrael I think it is not really a duplicate given that the dfs have diferent columns – JamesHudson81 Aug 22 '19 at 09:02
  • hmmm, why do you think so? – jezrael Aug 22 '19 at 09:03
  • 3
    @jezrael: because here a column is replaced with values in another dataframe that acts as some sort of dictionary. It is not joining two columns together. Joining can help here, although there exists a more simple solution, like `df1['Id'].replace(df2.set_index('Id')['value'])`. – Willem Van Onsem Aug 22 '19 at 09:05
  • @WillemVanOnsem - You are right, dupe is changed. Thank you. – jezrael Aug 22 '19 at 09:10

1 Answers1

1

try this,

df1['Value'] = df1['Id'].map(ref_df.set_index('Id')['Value'])

O/P:

   Id  Value
0  so      3
1  fe      3
2  dl      1
Mohamed Thasin ah
  • 10,754
  • 11
  • 52
  • 111