python - Converting column names of a dataframe based on a table -


suppose have dataframe this

   100  101  102 1  0.5  0.1  0.2  2  0.3  0.4  0.5 3  0.1  0.1  0.1 

and table this:

1 100 2 101 b 3 102 c 

what want is:

      b     c 1  0.5  0.1  0.2  2  0.3  0.4  0.5 3  0.1  0.1  0.1 

i'll borrow @amitavory's assumption second frame has columns x , y (it's trivially changed). can use rename method , columns argument:

>>> df_new = df0.rename(columns=dict(zip(df1.x, df1.y))) >>> df_new         b    c 1  0.5  0.1  0.2 2  0.3  0.4  0.5 3  0.1  0.1  0.1 

this works because can use dictionary mapping old new names columns:

>>> dict(zip(df1.x, df1.y)) {100: 'a', 101: 'b', 102: 'c'} 

Comments