0

我正在尝试确定 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

你能告诉我为什么吗?

4

1 回答 1

1
  • SequenceMatcher不是为熊猫系列设计的。
  • 你可以.apply的功能。
  • SequenceMatcher例子
    • 偶数isjunk=None空间不被视为垃圾。
    • Withisjunk=lambda y: y == " "将空格视为垃圾。
from difflib import SequenceMatcher
import pandas as pd

data = {'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     ratio
0  Performance results achieved by the approaches submitted to this Challenge.  The six top approaches and three others outperform the strong baseline.  0.356164
1                    Accuracy is one of the basic principles of perfectionist.                                                              Where am I?  0.088235

# isjunk=None
df['ratio'] = df[['Text1', 'All']].apply(lambda x: SequenceMatcher(None, x[0], x[1]).ratio(), axis=1)

# display(df)
                                                                         Text1                                                                      All     ratio
0  Performance results achieved by the approaches submitted to this Challenge.  The six top approaches and three others outperform the strong baseline.  0.410959
1                    Accuracy is one of the basic principles of perfectionist.                                                              Where am I?  0.117647
于 2020-08-12T18:44:25.027 回答