我有一个大的数据集all_transcripts
,有将近300万行。其中一列msgText
包含书面消息。在
>>> all_transcripts['msgText']
['this is my first message']
['second message is here']
['this is my third message']
此外,我有一个包含200多个单词的列表,名为gemeentes
。在
如果此列表中的某个词包含在msgText
中,我想用另一个词替换它。为此,我创建了一个函数:
def replaceCity(text):
newText = text.replace(plaatsnaam, 'woonplaats')
return str(newText)
因此,我的期望输出如下:
['this is my woonplaats message']
['woonplaats message is here']
['this is my woonplaats message']
目前,我正在遍历列表,并对列表中的每个项目应用replaceCity
函数。在
for plaatsnaam in gemeentes:
global(plaatsnaam)
all_transcripts['filtered_text'] = test.msgText.apply(replaceCity)
但是,这需要很长时间,所以似乎没有效率。有没有更快的方法来完成这个任务?在
这篇文章(Algorithm to find multiple string matches)很相似,但是我的问题不同,因为:
这里只有一大块小文字,而我有一个 包含许多不同行的数据集
我想替换单词,而不仅仅是查找单词。
假设
all_transcripts
是熊猫DataFrame
:示例:
^{pr2}$输出
相关问题 更多 >
编程相关推荐