Gerard
Gerard

Reputation: 25

Replace Index values with matched values in other dataframe

I have the following DataFrame:

df1:

   Index   ColA  
     a      10   
     b       9   
     c      12   
     d      13   

df2:

   Index  Letter  Names    
     0      d     Waylon    
     1      a     Marcus 
     2      z     Eddie 
     3      q     Justine 
     4      c     Angela 
     5      b     Joanna 

I want to make to replace the index values in df1 with the corresponding values in df2, so the result would look like:

df1:

   Index   ColA  
   Marcus   10   
   Joanna    9   
   Angela   12   
   Waylon   13   

Any ideas?

Upvotes: 1

Views: 91

Answers (1)

jezrael
jezrael

Reputation: 862541

You need rename by dictionary created by set_index with to_dict:

df1 = df1.rename(index=df1.set_index('Letter')['Names'].to_dict())

Upvotes: 1

Related Questions