我有两个包含列表列的数据框。
我想根据列表上的2+共享值加入。例如:
ColumnA ColumnB | ColumnA ColumnB
id1 ['a','b','c'] | id3 ['a','b','c','x','y', 'z']
id2 ['a','d,'e'] |
在本例中,我们可以看到,id1与id3匹配,因为列表上有2+个共享值。因此,输出将是(列名称并不重要,仅举个例子):
ColumnA1 ColumnB1 ColumnA2 ColumnB2
id1 ['a','b','c'] id3 ['a','b','c','x','y', 'z']
我怎样才能达到这个结果?我试图迭代dataframe#1中的每一行,但这似乎不是一个好主意。
谢谢大家!
如果您使用的是熊猫1.2.0或更新版本(于2020年12月26日发布),笛卡尔积(十字接头)可以简化如下:
另外,如果系统性能(执行时间)是您关心的问题,建议使用
list(map...
而不是较慢的apply(... axis=1)
使用
apply(... axis=1)
:使用
list(map(...
时:请注意,使用
list(map...
的速度快了3倍强>整套代码供您参考:
使用行的笛卡尔乘积并检查每行
代码是在线记录的
输出:
相关问题 更多 >
编程相关推荐