我试图从2万条评论中找到并替换单词。查找和替换单词存储在dataframe中,它大约有20000多个。不同数据帧中的注释,其值约为20K
下面是一个例子
import pandas as pd
df1 = pd.DataFrame({'Data' : ["Hull Damage happened and its insured by maritime hull insurence company","Non Cash Entry and claims are blocked"]})
df2 = pd.DataFrame({ 'Find' : ["Insurence","Non cash entry"],
'Replace' : ["Insurance","Blocked"],
})
我期待下面的输出
^{pr2}$请帮忙。在
我正在使用循环,但它需要20多分钟才能完成。 数据中有20K条记录,需要替换30000字
“KeywordSynonym”--Dataframe保存sql中的查找和替换数据
“备份”--数据帧保存要清理的数据
backup = str(backup)
TrainingClaimNotes_KwdSyn = []
for index,row in KeywordSynonym.iterrows():
word = KeywordSynonym.Synonym[index].lower()
value = KeywordSynonym.Keyword[index].lower()
my_regex = r"\b(?=\w)" + re.escape(word) + r"\b(?!\w)"
if re.search(my_regex,backup):
backup = re.sub(my_regex, value, backup)
TrainingClaimNotes_KwdSyn.append(backup)
TrainingClaimNotes_KwdSyn_Cmp = backup.split('\'", "\'')
使用:
输出
^{pr2}$说明
dict(zip(df2['Find'].str.lower(), df2['Replace'].str.lower()))
在要替换的内容和要替换的字符串之间创建一个映射-{'insurence': 'insurance', 'non cash entry': 'blocked'}
将查找转换为
regex
,使其可以进行查找-最后一件事就是做真正的替代品-
注意:为了找到合适的匹配项,我到处做了
.lower()
。很明显你可以把它重塑成你想要的样子。在相关问题 更多 >
编程相关推荐