简体   繁体   中英

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.

eg word_data with columns word1, word, word3, word4....

I am using pandas processing my data.

Thanks

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM