1

I have two dataframes:

df1 = [ A     B
        X    y1
        XX   y2 ]

df2 = [ X    XX
        1    2
        2    3 ]

I want to replace the names of the df2 (X, XX) with the values of the same index in df1. So my result will be:

df2 = [ y1    y2
         1    2
         2    3 ]
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
SmackCat
  • 41
  • 4

1 Answers1

2

Make df1 into a series of column B indexed by column A, and use that to rename the columns of df2.

renamed = df2.rename(columns=df1.set_index("A")["B"])
Stuart
  • 9,597
  • 1
  • 21
  • 30