Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
861 views
in Technique[技术] by (71.8m points)

python - Find the similarity between two string columns of a DataFrame

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

Data frame is like below:

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

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).

Expected Output is:

Col-2              Output
have a account     Account(85),AccountDTH(80),Balance(60)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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']])

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

The output of the above code:

         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)]

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...