Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-比较姓氏列并获得每行的最大相似性_Python - Fatal编程技术网

Python-比较姓氏列并获得每行的最大相似性

Python-比较姓氏列并获得每行的最大相似性,python,Python,我有一张双人桌。目的是比较姓氏。然而,其中一些人的姓氏是双筒的,已经分成两列。我想对这些姓氏进行所有可能的比较,并获得它们的最大相似性 例如: 我有这张桌子 +-----------+-----------+------------+-----------+-----------+------------+ |person1_id |lastname1_1|lastname1_2 |person2_id |lastname2_1|lastname2_2 | +-----------+------

我有一张双人桌。目的是比较姓氏。然而,其中一些人的姓氏是双筒的,已经分成两列。我想对这些姓氏进行所有可能的比较,并获得它们的最大相似性

例如: 我有这张桌子

+-----------+-----------+------------+-----------+-----------+------------+
|person1_id |lastname1_1|lastname1_2 |person2_id |lastname2_1|lastname2_2 |
+-----------+-----------+------------+-----------+-----------+------------+
|1          |Johnson    |null        |6          |Johnson    |null        |
|2          |Smith      |Dorrien     |7          |Smith      |null        |
|3          |Scott      |null        |8          |Garcia     |Scott       |
|4          |Morris     |null        |9          |Flores     |null        |
|5          |Foster     |null        |10         |Nelson     |null        |
+-----------+-----------+------------+-----------+-----------+------------+
最好的结果是:

+-----------+-----------+------------+-----------+-----------+------------+----------+
|person1_id |lastname1_1|lastname1_2 |person2_id |lastname2_1|lastname2_2 |similarity|
+-----------+-----------+------------+-----------+-----------+------------+----------+
|1          |Johnson    |null        |6          |Johnson    |null        |1.0       |
|2          |Smith      |Dorrien     |7          |Smith      |null        |1.0       |
|3          |Scott      |null        |8          |Garcia     |Scott       |1.0       |
|4          |Morris     |null        |9          |Flores     |null        |0.5       |
|5          |Foster     |null        |10         |Nelson     |null        |0.16      |
+-----------+-----------+------------+-----------+-----------+------------+----------+
有什么方法可以实现这一点吗


谢谢你。

这应该可以。首先,重新创建您的数据,以便您可以看到我正在测试什么

import pandas as pd

person_one_first_surname_column = ["Johnson", "Smith", "Scott", "Morris", "Foster"]
person_two_first_surname_column = ["Johnson", "Smith", "Garcia", "Flores", "Nelson"]
person_one_second_surname_column = ["null", "Dorrien", "null", "null", "null"] 
person_two_second_surname_column = ["null", "null", "Scott", "null", "null"]



dataset = {'lastname1_1': person_one_first_surname_column, 'lastname1_2': person_one_second_surname_column, "lastname2_1" : person_two_first_surname_column, "lastname2_2": person_two_second_surname_column}
df = pd.DataFrame(data=dataset)
将来,如果您以代码格式包含示例数据,以节省帮助您的人员的时间,这将非常有用!我不确定如何处理“null”值,所以假设它们也是字符串

我们首先定义一个比较两个名称列表的函数。它的工作原理是创建一个新的对列表
(a,b)
,其中
a
来自第一个列表,
b
来自第二个列表,并且仅当它们不等于
“null”
时才包括它们。然后,在从该列表中获取最大值之前,它对它们运行序列匹配器,并获取比率

import difflib
def get_max_similarity(list_of_user_one_names, list_of_user_two_names):
    max_similarity = max([difflib.SequenceMatcher(None, a,b).ratio() for a in list_of_user_one_names if a != "null" for b in list_of_user_two_names if b != "null"])
    return max_similarity
现在,我们使用apply函数在数据帧的每一行上调用新函数,将名称列表作为变量输入。我们将这个新数据作为新行“Max_相似度”分配给数据帧

输出:

  lastname1_1 lastname1_2 lastname2_1 lastname2_2  Max_similarity
0     Johnson        null     Johnson        null        1.000000
1       Smith     Dorrien       Smith        null        1.000000
2       Scott        null      Garcia       Scott        1.000000
3      Morris        null      Flores        null        0.500000
4      Foster        null      Nelson        null        0.166667

你的桌子是什么格式的?这是数据库还是存储在数据框中?还有,你是如何定义相似性的?测量两条弦之间的相似性或距离有很多不同的方法。嗨,安德鲁。这是一个数据帧。在这种情况下,我想使用SequenceMatcher的相似性就可以了。@VickyK很高兴我能帮上忙。
  lastname1_1 lastname1_2 lastname2_1 lastname2_2  Max_similarity
0     Johnson        null     Johnson        null        1.000000
1       Smith     Dorrien       Smith        null        1.000000
2       Scott        null      Garcia       Scott        1.000000
3      Morris        null      Flores        null        0.500000
4      Foster        null      Nelson        null        0.166667