问题
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