【发布时间】:2020-04-16 02:24:41
【问题描述】:
我有两个 csv 文件,其中一个包含如下所示的句子:
sentences
0 yes good bye how should and bye
1 bye should
2 good bye
还有另一个 csv,其中包含每个单词及其旁边的索引,如下所示:
word frequency index
0 and 500 10
1 you 334 1
2 how 320 2
3 should 250 3
4 yes 100 4
5 bye 50 5
6 good 1 6
我正在尝试使用 Dictionary 作为我的问题的解决方案,但它只打印一个单词而不是整个句子的奇怪输出
import string
import pandas as pd
text=pd.read_csv("one.csv")
change=pd.read_csv("result.csv")
print(text)
update = dict(zip(change.word, change.index))
print(update)
text1 = text['sentences'].replace(update, regex=True)
print(text1)
text1.to_csv('yes.csv', header=False, index=False)
我希望输出是:
4 6 5 2 3 10 5
5 3
6 5
我得到的是这个输出:
我在做什么错任何解决方案?
【问题讨论】:
标签: python string pandas csv str-replace