PeterB
PeterB

Reputation: 2414

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.

Here is first 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:

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:

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.

Upvotes: 2

Views: 82

Answers (1)

akuiper
akuiper

Reputation: 214957

If the id matches in order, you can add the two columns:

df1.fruit_list = df1.fruit_list + df2.fruit_list
df1

enter image description here

Or if need to merge on id column, you can merge, and then add the fruit_list columns:

(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))

enter image description here

Upvotes: 3

Related Questions