简体   繁体   中英

Python Pandas lookup and replace df1 value from df2

I have two dataframes

df df2

df column FOUR matches with df2 column LOOKUP COL

I need to match df column FOUR with df2 column LOOKUP COL and replace df column FOUR with the corresponding values from df2 column RETURN THIS

The resulting dataframe could overwrite df but I have it listed as result below.

NOTE: THE INDEX DOES NOT MATCH ON EACH OF THE DATAFRAMES

df = pd.DataFrame([['a', 'b', 'c', 'd'], 
              ['e', 'f', 'g', 'h'],
              ['j', 'k', 'l', 'm'],
              ['x', 'y', 'z', 'w']])

df.columns = ['ONE', 'TWO', 'THREE', 'FOUR']

      ONE  TWO THREE FOUR
   0    a   b   c   d
   1    e   f   g   h
   2    j   k   l   m
   3    x   y   z   w 

df2 = pd.DataFrame([['a', 'b', 'd', '1'], 
              ['e', 'f', 'h', '2'],
              ['j', 'k', 'm', '3'],
              ['x', 'y', 'w', '4']])

df2.columns = ['X1', 'Y2', 'LOOKUP COL', 'RETURN THIS']

    X1  Y2  LOOKUP COL  RETURN THIS
 0  a   b      d              1
 1  e   f      h              2
 2  j   k      m              3
 3  x   y      w              4

RESULTING DF

   ONE  TWO THREE  FOUR
0   a   b     c     1
1   e   f     g     2
2   j   k     l     3
3   x   y     z     4

You can use Series.map. You'll need to create a dictionary or a Series to use in map. A Series makes more sense here but the index should be LOOKUP COL :

df['FOUR'] = df['FOUR'].map(df2.set_index('LOOKUP COL')['RETURN THIS'])

df
Out: 
  ONE TWO THREE FOUR
0   a   b     c    1
1   e   f     g    2
2   j   k     l    3
3   x   y     z    4
df['Four']=[df2[df2['LOOKUP COL']==i]['RETURN THIS'] for i in df['Four']]

Should be something like sufficient to do the trick? There's probably a more pandas native way to do it.

Basically, list comprehension - We generate a new array of df2['RETURN THIS'] values based on using the lookup column as we iterate over the i in df['Four'] list.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM