我有以下问题
我有一个数据框master,其中包含诸如以下句子:
我不知道为什么不能同时处理多个行的比较。
问题是:我不知道如何做到这一点,或者是否可能。
非常感谢任何帮助!
我有一个数据框master,其中包含诸如以下句子:
master
Out[8]:
original
0 this is a nice sentence
1 this is another one
2 stackoverflow is nice
对于Master中的每一行,我使用fuzzywuzzy
在另一个数据帧中查找最佳匹配。这个数据帧被称为slave。我使用fuzzywuzzy是因为两个数据帧之间匹配的句子可能会有些不同(额外的字符等)。
例如,slave可以是:
slave
Out[10]:
my_value name
0 2 hello world
1 1 congratulations
2 2 this is a nice sentence
3 3 this is another one
4 1 stackoverflow is nice
这里是一个完全功能的,极好的,紧凑的工作示例 :)
from fuzzywuzzy import fuzz
import pandas as pd
import numpy as np
import difflib
master= pd.DataFrame({'original':['this is a nice sentence',
'this is another one',
'stackoverflow is nice']})
slave= pd.DataFrame({'name':['hello world',
'congratulations',
'this is a nice sentence ',
'this is another one',
'stackoverflow is nice'],'my_value': [2,1,2,3,1]})
def fuzzy_score(str1, str2):
return fuzz.token_set_ratio(str1, str2)
def helper(orig_string, slave_df):
#use fuzzywuzzy to see how close original and name are
slave_df['score'] = slave_df.name.apply(lambda x: fuzzy_score(x,orig_string))
#return my_value corresponding to the highest score
return slave_df.ix[slave_df.score.idxmax(),'my_value']
master['my_value'] = master.original.apply(lambda x: helper(x,slave))
这个问题的价值是一百万美元:我能并行化上面的应用程序代码吗?
毕竟,每一行在master
中都要与slave
中的所有行进行比较(slave数据集很小,我可以将许多副本保存到RAM中)。我不知道为什么不能同时处理多个行的比较。
问题是:我不知道如何做到这一点,或者是否可能。
非常感谢任何帮助!