简体   繁体   English

从 dataframe 列获取最相似的值到特定字符串 python

[英]Get most similar value from dataframe column to specific string python

I want to find the most similar value from a dataframe column to a specified string, eg a='book' .我想从 dataframe 列中找到与指定字符串最相似的值,例如a='book' Let's say the dataframe looks like: df假设 dataframe 看起来像: df

col1
wijk 00 book
Wijk a 
test

Now I want to return wijk 00 book since this is the most similar to a .现在我想返回wijk 00 book ,因为这a . I am trying to do this with the fuzzywuzzy package.我正在尝试使用模糊的fuzzywuzzy来做到这一点。

Therefore, I have a dataframe A with the values I want to have a similar one for.因此,我有一个 dataframe A的值,我想要一个类似的值。 Then I use:然后我使用:

A['similar_value'] = A.col1.apply(lambda x: [process.extract(x, df.col1, limit=1)][0][0][0])  

But when comparing a lot of strings, this takes too much time.但是当比较很多字符串时,这会花费太多时间。 Does anyone knows how to do this quickly?有谁知道如何快速做到这一点?

I would use rapidfuzz :我会使用rapidfuzz

from rapidfuzz import process, fuzz

df = pd.DataFrame(['wijk 00 book', 'Wijk a', 'test'], columns=['col1'])

search_str = 'book'
most_similar = process.extractOne(search_str, df['col1'], scorer=fuzz.WRatio)

Output: Output:

most_similar
('wijk 00 book', 90.0, 0)

This gives you the most similar string in the column as well as a score for how similar it is to your search string.这会为您提供列中最相似的字符串以及它与您的搜索字符串的相似程度的分数。

You can use 'str.contains' method to get the string which exact substring您可以使用“str.contains”方法获取与 substring 完全相同的字符串

df[df["column_name"].str.contains("book")].values[0][0]

Try fuzz.ratio() with Series.idxmax() .尝试fuzz.ratio()Series.idxmax() This will locate the col1 value with the highest fuzzy score against a :这将针对a定位具有最高模糊分数的col1值:

from rapidfuzz import fuzz

a = 'book'
df.loc[df.col1.apply(lambda x: fuzz.ratio(x, a)).idxmax()]

# col1    wijk 00 book
# Name: 0, dtype: object

Update: The process.extractOne() method from @lolliesaurus is faster:更新: @lolliesaurusprocess.extractOne()方法更快:

>>> %timeit process.extractOne(a, df.col1, scorer=fuzz.WRatio)
11.6 µs ± 180 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

>>> %timeit df.loc[df.col1.apply(lambda x: fuzz.ratio(x, a)).idxmax()]
353 µs ± 3.45 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM