gpt4 book ai didi

python - 字数列表无法重命名列名称

转载 作者:行者123 更新时间:2023-12-01 01:37:10 25 4
gpt4 key购买 nike

我有这段代码可以读取“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 个元素,但原因是因为它将“word”列视为索引,知道如何解决这个问题吗?

最佳答案

输出是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'})

关于python - 字数列表无法重命名列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52288243/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com