replace column names from one df with the rows from the other df

Question:

df1:

word merged
green positive_green
green energy positive_green_energy
jets negative_jets
green hydrogen positive_green_hydrogen
renewable energy positive_renewable_energy

df2:

column1 column2 green green energy jets green hydrogen renewable energy
xx xx xx xx xx xx xx

I would like to replace columns’ names in the df2 to the ones from df1 (rows from df1 matching columns with df2 and replace)

Asked By: Kas

||

Answers:

Use DataFrame.rename with dictionary:

df2 = df2.rename(columns=dict(zip(df1.word, df1.merged)))
Answered By: jezrael
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.