Home > Back-end >  How to join two columns with lists into column with nested lists?
How to join two columns with lists into column with nested lists?

Time:05-14

I have a dataframe:

v1          v2
[1,2]     [4,5,6]
[1,1,5]   [4,5,6,7]

I want to join them into column with nested lists:

v1          v2        v3
[1,2]     [4,5,6]    [[1,2],[4,5,6]]
[1,1,5]   [4,5,6,7]  [[1,1,5],[4,5,6,7]]

CodePudding user response:

You can try this :

df['v3'] = df.apply(lambda row : [row['v1'], row['v2']], axis = 1)

print(df)

# Output :
#          v1            v2                         v3
#0     [1, 2]     [4, 5, 6]        [[1, 2], [4, 5, 6]]
#1  [1, 1, 5]  [4, 5, 6, 7]        [[1, 1, 5], [4, 5, 6, 7]]
  • Related