[英]How to combine two pandas.DataFrames which have lists as columns?
I am asking a questions similar to this one but I need something a bit different. 我要问的问题与此类似,但我需要有所不同。 I have two
pandas.DataFrames
and I need to merge them. 我有两个
pandas.DataFrames
,我需要将它们合并。
Here is first df_1
: 这是第一个
df_1
:
id attr fruit_list
---------------------------------------
0 42 [orange, apple]
1 57 [lemon]
2 86 [kiwi]
3 33 [pineapple, pear]
4 11 [apple, lemon]
and here is second df_2
: 这是第二个
df_2
:
id fruit_list
--------------------------------------
0 [fruit1,]
1 [fruit4, fruit2]
2 [fruit2, fruit8]
3 [fruit3,]
4 [fruit3,]
I need to merge those two DataFrames
to get output like this: 我需要合并这两个
DataFrames
以获取如下输出:
id attr fruit_list
---------------------------------------------
0 42 [orange, apple, fruit1]
1 57 [lemon, fruit4, fruit2]
2 86 [kiwi, fruit2, fruit8]
3 33 [pineapple, pear, fruit3]
4 11 [apple, lemon, fruit3]
How can I achieve this please? 请问我该如何实现? I have looked at documentation but I couldn't figure out a way.
我看过文档,但找不到办法。
If the id matches in order, you can add the two columns: 如果ID顺序匹配,则可以添加两列:
df1.fruit_list = df1.fruit_list + df2.fruit_list
df1
Or if need to merge on id
column, you can merge, and then add the fruit_list
columns: 或者,如果需要在
id
列上进行合并,则可以合并,然后添加fruit_list
列:
(df1.merge(df2, on = "id")
.assign(fruit_list = lambda x: x.fruit_list_x + x.fruit_list_y)
.drop(["fruit_list_x", "fruit_list_y"], 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.