Python 两个字符串列之间最接近的匹配
Python closest match between two string columns
我希望在两个单独的 table 中获得两列 string
数据类型之间的最接近匹配。我认为内容并不重要。我可以通过预处理数据(降低所有字母、替换空格和停用词等)并进行连接来匹配某些词。然而,我在 350 多个匹配项中得到了大约 80 个匹配项。重要的是要知道每个 table 的长度是不同的。
我曾尝试使用我在网上找到的一些代码,但它不起作用:
def Races_chien(df1,df2):
myList = []
total = len(df1)
possibilities = list(df2['Rasse'])
s = SequenceMatcher(isjunk=None, autojunk=False)
for idx1, df1_str in enumerate(df1['Race']):
my_str = ('Progress : ' + str(round((idx1 / total) * 100, 3)) + '%')
sys.stdout.write('\r' + str(my_str))
sys.stdout.flush()
# get 1 best match that has a ratio of at least 0.7
best_match = get_close_matches(df1_str, possibilities, 1, 0.7)
s.set_seq2(df1_str, best_match)
myList.append([df1_str, best_match, s.ratio()])
return myList
它说:TypeError: set_seq2() takes 2 positional arguments but 3 were given
我怎样才能完成这项工作?
您可以使用 jellyfish 库,该库具有比较两个字符串相似程度的有用工具,如果您正在寻找的话。
尝试更改:
s = SequenceMatcher(isjunk=None, autojunk=False)
收件人:
s = SequenceMatcher(None, isjunk=None, autojunk=False)
我认为你需要 s.set_seqs(df1_str, best_match)
函数而不是 s.set_seq2(df1_str, best_match)
(docs)
这是我终于得到的答案:
from fuzzywuzzy import process, fuzz
value = []
similarity = []
for i in df1.col:
ratio = process.extract(i, df2.col, limit= 1)
value.append(ratio[0][0])
similarity.append(ratio[0][1])
df1['value'] = pd.Series(value)
df1['similarity'] = pd.Series(similarity)
这会将与 df2 最接近的匹配值与 df1 中的相似度 %
相加
我希望在两个单独的 table 中获得两列 string
数据类型之间的最接近匹配。我认为内容并不重要。我可以通过预处理数据(降低所有字母、替换空格和停用词等)并进行连接来匹配某些词。然而,我在 350 多个匹配项中得到了大约 80 个匹配项。重要的是要知道每个 table 的长度是不同的。
我曾尝试使用我在网上找到的一些代码,但它不起作用:
def Races_chien(df1,df2):
myList = []
total = len(df1)
possibilities = list(df2['Rasse'])
s = SequenceMatcher(isjunk=None, autojunk=False)
for idx1, df1_str in enumerate(df1['Race']):
my_str = ('Progress : ' + str(round((idx1 / total) * 100, 3)) + '%')
sys.stdout.write('\r' + str(my_str))
sys.stdout.flush()
# get 1 best match that has a ratio of at least 0.7
best_match = get_close_matches(df1_str, possibilities, 1, 0.7)
s.set_seq2(df1_str, best_match)
myList.append([df1_str, best_match, s.ratio()])
return myList
它说:TypeError: set_seq2() takes 2 positional arguments but 3 were given
我怎样才能完成这项工作?
您可以使用 jellyfish 库,该库具有比较两个字符串相似程度的有用工具,如果您正在寻找的话。
尝试更改:
s = SequenceMatcher(isjunk=None, autojunk=False)
收件人:
s = SequenceMatcher(None, isjunk=None, autojunk=False)
我认为你需要 s.set_seqs(df1_str, best_match)
函数而不是 s.set_seq2(df1_str, best_match)
(docs)
这是我终于得到的答案:
from fuzzywuzzy import process, fuzz
value = []
similarity = []
for i in df1.col:
ratio = process.extract(i, df2.col, limit= 1)
value.append(ratio[0][0])
similarity.append(ratio[0][1])
df1['value'] = pd.Series(value)
df1['similarity'] = pd.Series(similarity)
这会将与 df2 最接近的匹配值与 df1 中的相似度 %
相加