如何组合两个列表为列的pandas.DataFrame?

时间:2017-04-14 13:32:04

标签: python pandas dataframe

我问的问题类似于this one,但我需要一些不同的东西。我有两个pandas.DataFrames,我需要合并它们。

这是第一个df_1

id       attr         fruit_list
---------------------------------------
0        42           [orange, apple]
1        57           [lemon]
2        86           [kiwi]
3        33           [pineapple, pear]
4        11           [apple, lemon]

这是第二个df_2

id     fruit_list
--------------------------------------
0      [fruit1,]
1      [fruit4, fruit2]
2      [fruit2, fruit8]
3      [fruit3,]
4      [fruit3,]

我需要合并这两个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]

我怎么能实现这个目标?我看过documentation,但我找不到办法。

1 个答案:

答案 0 :(得分:3)

如果id按顺序匹配,您可以添加两列:

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

enter image description here

或者如果需要在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))

enter image description here