Python比较姓氏列并获得它们的最大相似性p

2024-06-17 08:27:14 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一张双人桌子。目的是比较姓氏。然而,其中一些人的姓是双筒的,已经分成两列。我想做所有可能的比较,在这些姓氏和得到最大的相似性。你知道吗

例如: 我有这张桌子

+-----------+-----------+------------+-----------+-----------+------------+
|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      |
+-----------+-----------+------------+-----------+-----------+------------+----------+

有什么办法可以实现吗?你知道吗

谢谢你。你知道吗


Tags: idnullscottsmithmorris桌子姓氏johnson
1条回答
网友
1楼 · 发布于 2024-06-17 08:27:14

这应该能奏效。首先只是重新创建你的数据,这样你就可以看到我在测试什么。你知道吗

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\ u similarity”分配给数据帧。你知道吗

df["Max_similarity"] = df.apply(lambda row: get_max_similarity([row["lastname1_1"], row["lastname1_2"]], [row["lastname2_1"], row["lastname2_2"]]), axis=1)

输出:

  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

相关问题 更多 >