利用Pandas优化查找和替换数据帧

2024-09-30 08:17:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从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('\'", "\'') 

Tags: and数据redataframeindexmy单词backup
1条回答
网友
1楼 · 发布于 2024-09-30 08:17:18

使用:

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"],
                       }) 

find_repl = dict(zip(df2['Find'].str.lower(), df2['Replace'].str.lower()))
d2 = {r'(\b){}(\b)'.format(k):r'\1{}\2'.format(v) for k,v in find_repl.items()}

df1['Data_1'] = df1['Data'].str.lower().replace(d2, regex=True)

输出

^{pr2}$

说明

dict(zip(df2['Find'].str.lower(), df2['Replace'].str.lower()))在要替换的内容和要替换的字符串之间创建一个映射-

{'insurence': 'insurance', 'non cash entry': 'blocked'}

将查找转换为regex,使其可以进行查找-

d2 = {r'(\b){}(\b)'.format(k):r'\1{}\2'.format(v) for k,v in find_repl.items()}

{'(\\b)insurence(\\b)': '\\1insurance\\2', '(\\b)non cash entry(\\b)': '\\1blocked\\2'}

最后一件事就是做真正的替代品-

df1['Data_1'] = df1['Data'].str.lower().replace(d2, regex=True)

注意:为了找到合适的匹配项,我到处做了.lower()。很明显你可以把它重塑成你想要的样子。在

相关问题 更多 >

    热门问题