本文介绍了Python:在列表的列上合并两个Pandas数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要基于一列名为作者"的列表来找到两个熊猫数据框之间的交集.>在此处输入图片描述

I need to find intersection between two pandas dataframes based on a column of lists named "authors".enter image description here

但是我得到了这个错误在此处输入图片描述

but instead i get this errorenter image description here

推荐答案

您无法在列表上合并,因为列表无法散列,请参见.一种选择是通过将list转换为string并在其上合并来创建其他列,例如:

You cannot merge on a list, because list cannot be hashed, see this. One option would be to create an additional column by converting list to string and merge on it, e.g.:

df['authors_as_string'] = df['authors'].apply(lambda x: "-".join(x))

这将产生:

   id    authors authors_as_string
0   1  [a, b, c]          a-b-c
1   2  [a, b, c]          a-b-c
2   3     [a, b]            a-b
3   4     [a, c]            a-c

然后,您可以在第三列上进行合并.

Then you can merge on that third column.

或者,您可以尝试在该问题中发布的其他解决方案.

Alternatively you can try other solutions posted in that question.

这篇关于Python:在列表的列上合并两个Pandas数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 11:10
查看更多