Changing dataframe columns names by columns from another dataframe python

五迷三道 提交于 2021-01-29 09:03:31

问题


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


回答1:


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)



回答2:


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



来源:https://stackoverflow.com/questions/54647472/changing-dataframe-columns-names-by-columns-from-another-dataframe-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!