字数列表无法重命名列名

时间:2018-09-12 05:48:48

标签: python pandas dataframe

我有这段代码可以读取“ NAME”列,并返回每个单词的出现一个单词。

temp_df = pd.read_excel('file location here', index=True)

final_df = pd.Series(' '.join([unicode(i) for i in temp_df.NAME]).split()).value_counts()

问题是第一列,即即使我做类似的事情,单词的名称也总是默认情况下成为索引

final_df.rename({0: 'word', 1: 'count'})

它会告诉我仅存在1个元素,但是我正尝试重命名2个元素,但是原因是因为它将“单词”列视为索引,知道如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

输出为Series,因此需要Series.reset_index

final_df = final_df.reset_index()
final_df.columns = ['word', 'count']

另一种解决方案:

final_df = final_df.reset_index(name='count').rename(columns={'index':'word'})