的两个字符串列之间的相似性

的两个字符串列之间的相似性

本文介绍了查找 DataFrame 的两个字符串列之间的相似性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是编程新手.我有一个 Pandas 数据框,其中存在两个字符串列.

I am new to programming.I have a pandas data frame in which two string columns are present.

数据框如下:

Col-1             Col-2
Update            have a account
Account           account summary
AccountDTH        Cancel
Balance           Balance Summary
Credit Card       Update credit card

这里我需要检查 Col-2 元素与 Col-1 的每个元素的相似性.这意味着我必须将 have a accountCol-1 的所有元素进行比较.然后找到前3个相似的.假设相似度分数为:Account(85),AccountDTH(80),Balance(60),Update(45),Credit Card(35).

Here i need to check the similarity of Col-2 elements with each element of Col-1.It Means i have to compare have a account with all the elements of Col-1.Then find the top 3 similar one. Suppose the similarity scores are :Account(85),AccountDTH(80),Balance(60),Update(45),Credit Card(35).

预期输出为:

Col-2              Output
have a account     Account(85),AccountDTH(80),Balance(60)

推荐答案

您可以使用 Python 库,例如 在这里,fuzzywuzzy 支持这种类型的任务:

You can use a Python library like fuzzywuzzy here, which has support for this type of task:

from fuzzywuzzy import process

df.assign(Output=[process.extract(i, df['Col-1'], limit=3) for i in df['Col-2']])

使用process 方法,我们可以得到字符串的相似度分数,然后选择前 3 个,如果 3 个存在:

Using the process method, we can get string similary scores, and then pick the top 3, if 3 exist:

以上代码的输出:

         Col-1               Col-2                                                         Output
0       Update      have a account       [(Account, 90, 1), (AccountDTH, 64, 2), (Update, 40, 0)]
1      Account     account summary  [(Account, 90, 1), (AccountDTH, 63, 2), (Credit Card, 38, 4)]
2   AccountDTH              Cancel      [(Balance, 62, 3), (Credit Card, 43, 4), (Update, 33, 0)]
3      Balance     Balance Summary      [(Balance, 90, 3), (Credit Card, 38, 4), (Update, 30, 0)]
4  Credit Card  Update credit card   [(Update, 90, 0), (Credit Card, 90, 4), (AccountDTH, 27, 2)]

为了加快比较速度(它本身使用 Python 的序列匹配器),我建议安装 python-Levenshtein

To speed this comparison up (natively it uses Python's sequence matcher), I would recommend installing python-Levenshtein

这篇关于查找 DataFrame 的两个字符串列之间的相似性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 12:58