Add dictionary values to dataframe column names

With this dataframe df

   a  b  c 
0  x  x  x
1  x  x  x
2  x  x  x

and this dictionary

headers = {'a' : '3.14', 'b' : '6.67', 'c' : '8.31'}

df = df.rename(headers) allows to rename the column names as such

   3.14  6.67  8.31 
0     x     x     x
1     x     x     x
2     x     x     x

How to just add the dictionary values to the column names? Like this:

   a 3.14  b 6.67  c 8.31 
0       x       x       x
1       x       x       x
2       x       x       x

>Solution :

Try with lambda

df = df.rename(columns = lambda x : x + ' ' +  headers.get(x))
Out[613]: 
  a 3.14 b 6.67 c 8.31
0      x      x      x
1      x      x      x
2      x      x      x

Leave a Reply