1

two column of 1 data frame and other two column of 2nd data frame now extract the value use by these two data frame

df1[["source", "Tar"]       df2["id", "me"]

source    Tar                  id        me
cd1        ef2                 60_2       ac4
cd1       hi1                 50_6        xg3
zx2        an2
cd1      60_2
zx2      50_6

Output should be

df1["source" , "Tar"]

source    Tar                  
cd1       ef2                 
cd1       hi1                 
zx2       an2
cd1       ac4
zx2       xg3
mozway
  • 194,879
  • 13
  • 39
  • 75
Rati Goyal
  • 69
  • 5

1 Answers1

0

You can perform a left merge and combine_first:

df1['Tar'] = (df1
              .merge(df2, left_on='Tar', right_on='id', how='left')
              ['me'].combine_first(df1['Tar'])
             )

Or map the values using a Series crafted from df2:

df1['Tar'] = df1['Tar'].map(df2.set_index('id')['me']).fillna(df1['Tar'])

output:

  source  Tar
0     cd  ef2
1     ab  hi1
2     zx  an2
3     yz  ac4
4     xy  xg3
mozway
  • 194,879
  • 13
  • 39
  • 75