Oldduck_
Oldduck_

Reputation: 1

Changing dataframe columns names by columns from another dataframe python

I have a dataframe valence_data with columns word1, word, word3, word4....

And I have my second dataframe word_data with columns 1, 2, ,3 ,4 ...

How can I replace the columns names in word_data by names from valence_data.

e.g. word_data with columns word1, word, word3, word4....

I am using pandas processing my data.

Thanks

Upvotes: 0

Views: 3048

Answers (2)

Genesis
Genesis

Reputation: 50

Just do this

import pandas as pd
word_data = pd.DataFrame(word_data,columns=list(valence_data))

But the number of columns in both dataframes should be equal

Upvotes: 0

cmaureir
cmaureir

Reputation: 285

You need to use DataFrame.rename

original_names = ["1", "2", ...]
new_names = ["word1", "word2", ...]
new_columns = dict(zip(original_names, new_names))
df.rename(index=str, columns=new_columns)

Upvotes: 1

Related Questions