Home > OS >  replace column names from one df with the rows from the other df
replace column names from one df with the rows from the other df

Time:11-28

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)

CodePudding user response:

Use DataFrame.rename with dictionary:

df2 = df2.rename(columns=dict(zip(df1.word, df1.merged)))
  • Related