检查同一列中是否有类似的字符串

Kal*_*lol 2 python fuzzy-search pandas

我有一个这样的数据框,

df
col1             col2
 A        'the value is zero'
 B        'this is a cat'
 C        'the value is one'
 D        'nothing is here'
 E        'the colour is blue'
 F        'this is dog'
 G        'empty sequence'
 H        'the colour is red'
 I        'the colour is green'         1
Run Code Online (Sandbox Code Playgroud)

现在我想要类似类型的字符串标记为 1,其他标记为零,所以最终的数据框应该是这样的,

col1             col2                 col1
 A        'the value is zero'           1
 B        'this is a cat'               1
 C        'the value is one'            1
 D        'nothing is here'             0
 E        'the colour is blue'          1
 F        'this is dog'                 1
 G        'empty sequence'              0
 H        'the colour is red'           1
 I        'the colour is green'         1 
Run Code Online (Sandbox Code Playgroud)

0 和 1 可以使用 SequenceMatcher(SequenceMatcher(None, s1, s2).ratio()) 函数获得,并且通过一些阈值,我们可以将其设为 0 或 1。

但是如果我使用 for 循环来查找彼此之间的相似性,那么执行将需要更长的时间。寻找一些 Pandas 快捷方式/pythonic 方式来有效地做到这一点。

yat*_*atu 5

类似于是否可以与 python pandas 进行模糊匹配合并?,我们可以通过difflib查看由 返回的列表的长度来使用并检查是否找到了 1 个以上的相似字符串(以排除它自己的字符串)difflib.get_close_matches

import difflib

df['col1'] = [(len(difflib.get_close_matches(x, df['col2'], cutoff=0.7))>1)*1 
              for x in df['col2']]
Run Code Online (Sandbox Code Playgroud)
print(df)

   col1                            col2
0     1             'the value is zero'
1     1                 'this is a cat'
2     1              'the value is one'
3     0               'nothing is here'
4     1            'the colour is blue'
5     1                   'this is dog'
6     0                'empty sequence'
7     1             'the colour is red'
8     1           'the colour is green'        
Run Code Online (Sandbox Code Playgroud)

基于模糊匹配的相似度矩阵

人们也可能对获得一个相似度矩阵感兴趣,该矩阵将旋转列中的所有值设置1为字符串是否相似。为此,我们可以像上面一样继续进行,但保留整个列表,将其分解并使用pd.crosstab以下方法旋转结果数据帧:

df['sim'] = [difflib.get_close_matches(x, df['col2'], cutoff=0.7)  for x in df['col2']]
sim_df = df.explode('sim')
pd.crosstab(sim_df.col2, sim_df.sim)

sim             empty sequence  nothing is here  the colour is blue... the value is zero  this is a cat  this is dog
col2
empty sequence      1                0                     0         ...        0                   0            0
nothing is here     0                1                     0         ...        0                   0            0
the colour is blue  0                0                     1         ...        0                   0            0
the colour is green 0                0                     1         ...        0                   0            0
the colour is red   0                0                     1         ...        0                   0            0
the value is one    0                0                     0         ...        1                   0            0
the value is zero   0                0                     0         ...        1                   0            0
this is a cat       0                0                     0         ...        0                   1            1
this is dog         0                0                     0         ...        0                   1            1 
Run Code Online (Sandbox Code Playgroud)