25

I have a pandas DataFrame and I would like to rename the columns based on another DataFrame that I plan to use as dictionary.

For example, the first DataFrame is:

          AAA   BBB   CCC   DDD
 index   
  1       1     2     3     4
  2       5     6     7     8

and the second DataFrame that I would like to use as the dictionary:

           val1    val2
  index
    1      AAA      A7
    2      BBB      B0
    3      CCC      C3
    4      DDD      D1

What I would like to get as result is the following:

          A7    B0    C3    D1 
 index   
  1       1     2     3     4
  2       5     6     7     8

Initially I thought to reshape the first DataFrame to long format, then merge with the dictionary DataFrame and then reshape back to wide format. However I think this is quite inefficient, so I would like to use a more efficient way (if one exists). Thank you very much four your help.

mirekphd
  • 4,799
  • 3
  • 38
  • 59
km1234
  • 2,127
  • 4
  • 18
  • 21

4 Answers4

46

df.rename has a parameter called columns that accepts dictionaries:

df.rename(columns=dict(zip(df2["val1"], df2["val2"])))

Out:

    A7  B0  C3  D1
0   1   2   3   4
1   5   6   7   8

It returns a new DataFrame. You can either use inplace=True, or assign it back to the original DataFrame.

ayhan
  • 70,170
  • 20
  • 182
  • 203
14

I think you can first create dictionary from df2, then create Series from columns of df1 by to_series which you then map using dictionary:

print df1
       AAA  BBB  CCC  DDD
index                    
1        1    2    3    4
2        5    6    7    8

print df2
      val1 val2
index          
1      AAA   A7
2      BBB   B0
3      CCC   C3
4      DDD   D1

d = df2.set_index('val1').to_dict()
print d['val2']
{'AAA': 'A7', 'BBB': 'B0', 'CCC': 'C3', 'DDD': 'D1'}

df1.columns = df1.columns.to_series().map(d['val2'])
print df1
       A7  B0  C3  D1
index                
1       1   2   3   4
2       5   6   7   8
Ezra Citron
  • 101
  • 2
  • 6
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0
df1 = df1.rename(columns=dict(df2.values))
Sumit Pokhrel
  • 2,087
  • 24
  • 18
-4

You can also just use this one line:

df1.columns=[df2['val2']]

Output:

    A7  B0  C3  D1
0   1   2   3   7
1   5   6   7   8
Joe T. Boka
  • 6,554
  • 6
  • 29
  • 48
  • That is not renaming via dictionary, i.e. Old name => New Name. Your solution renames the columns by position. – RufusVS Sep 20 '18 at 22:36