Home > Software design >  how to append elements in a list in dataframe to elements in a list in another dataframe?
how to append elements in a list in dataframe to elements in a list in another dataframe?

Time:07-31

I want to append values of a column in df1 append to values of a column in df2 and the same as for the 'b' column. the result should be:

enter image description here

CodePudding user response:

Use pd.combine for this:

df1 = pd.DataFrame({
    'a' : [[*range(5)]],
    'b' : [[*range(5,10)]]
})
df2 = pd.DataFrame({
    'a' : [[*range(11,14)]],
    'b' : [[*range(22,25)]]
})

result = df1.combine(df2, lambda x, y: x   y)

Output result:

                             a                            b
0  [0, 1, 2, 3, 4, 11, 12, 13]  [5, 6, 7, 8, 9, 22, 23, 24]
  • Related