【发布时间】:2016-10-25 01:16:00
【问题描述】:
我有以下问题
我有一个包含句子的数据框 master,例如
master
Out[8]:
original
0 this is a nice sentence
1 this is another one
2 stackoverflow is nice
对于 Master 中的每一行,我使用 fuzzywuzzy 查找另一个 Dataframe slave 以获得最佳匹配。我使用了fuzzywuzzy,因为两个数据帧之间的匹配句子可能会有所不同(额外的字符等)。
例如,从属可以是
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))
100 万美元的问题是:我可以并行化上面的应用代码吗?
毕竟,master 中的每一行都与slave 中的所有行进行比较(从属数据集是一个小数据集,我可以将许多数据副本保存到 RAM 中)。
我不明白为什么我不能运行多重比较(即同时处理多行)。
问题:我不知道该怎么做,或者这是否可能。
非常感谢任何帮助!
【问题讨论】:
-
我注意到您在此处添加了 dask 标签。您是否尝试过使用 dask 并遇到问题?
-
感谢您的帮助!看来 dask 只接受常规功能
-
Dask 使用 cloudpickle 来序列化函数,因此可以轻松处理其他数据集上的 lambda 和闭包。
-
差不多,但我会使用
assign而不是列分配,我会向apply提供有关您期望的列的元数据。如果您创建一个最小的可重现示例,那么提供明确的解决方案会更容易。例如,我可以复制粘贴到本地机器上。
标签: python pandas parallel-processing dask fuzzywuzzy