我有一张双人桌子。目的是比较姓氏。然而,其中一些人的姓是双筒的,已经分成两列。我想做所有可能的比较,在这些姓氏和得到最大的相似性。你知道吗
例如: 我有这张桌子
+-----------+-----------+------------+-----------+-----------+------------+
|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 |
+-----------+-----------+------------+-----------+-----------+------------+----------+
有什么办法可以实现吗?你知道吗
谢谢你。你知道吗
这应该能奏效。首先只是重新创建你的数据,这样你就可以看到我在测试什么。你知道吗
在将来,如果您将示例数据包含在代码格式中以节省帮助您的人的时间,这将是很有帮助的!我不确定如何处理“null”值,所以假设它们也是字符串。你知道吗
我们首先定义一个比较两个名称列表的函数。它的工作原理是创建一个新的成对列表
(a,b)
,其中a
来自第一个列表,b
来自第二个列表,并且仅当它们不等于"null"
时才包含它们。然后对它们运行序列匹配器,并获取比率,然后从该列表中获取最大值。你知道吗我们现在使用apply函数在数据帧的每一行上调用新函数,将名称列表作为变量输入。我们将这个新数据作为新行“Max\ u similarity”分配给数据帧。你知道吗
输出:
相关问题 更多 >
编程相关推荐