使用 SequenceMatcher 比较 pandas 中两列中的字符串

我正在尝试确定熊猫数据框中两列的相似性:


Text1                                                                             All

Performance results achieved by the approaches submitted to this Challenge.       The six top approaches and three others outperform the strong baseline.

Accuracy is one of the basic principles of perfectionist.                             Where am I?

我想比较'Performance results ... 'with'The six...'和 ' Accuracy is one...'with 'Where am I?'。第一行应该有较高的两列之间的相似度,因为它包含一些词;第二个应该等于 0,因为两列之间没有共同的单词。


要比较我使用的两列,SequenceMatcher如下所示:


from difflib import SequenceMatcher


ratio = SequenceMatcher(None, df.Text1, df.All).ratio()

但是 . 的使用似乎是错误的df.Text1, df.All。


你能告诉我为什么吗?


繁华开满天机
浏览 168回答 1
1回答

30秒到达战场

SequenceMatcher不是为熊猫系列设计的。你可以.apply的功能。SequenceMatcher例子偶数空格isjunk=None不被认为是垃圾。Withisjunk=lambda y: y == " "将空格视为垃圾。from difflib import SequenceMatcherimport pandas as pddata = {'Text1': ['Performance results achieved by the approaches submitted to this Challenge.', 'Accuracy is one of the basic principles of perfectionist.'],        'All': ['The six top approaches and three others outperform the strong baseline.', 'Where am I?']}df = pd.DataFrame(data)# isjunk=lambda y: y == " "df['ratio'] = df[['Text1', 'All']].apply(lambda x: SequenceMatcher(lambda y: y == " ", x[0], x[1]).ratio(), axis=1)# display(df)                                                                         Text1                                                                      All     ratio0  Performance results achieved by the approaches submitted to this Challenge.  The six top approaches and three others outperform the strong baseline.  0.3561641                    Accuracy is one of the basic principles of perfectionist.                                                              Where am I?  0.088235# isjunk=Nonedf['ratio'] = df[['Text1', 'All']].apply(lambda x: SequenceMatcher(None, x[0], x[1]).ratio(), axis=1)# display(df)                                                                         Text1                                                                      All     ratio0  Performance results achieved by the approaches submitted to this Challenge.  The six top approaches and three others outperform the strong baseline.  0.4109591                    Accuracy is one of the basic principles of perfectionist.                                                              Where am I?  0.117647
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python