'How to join two columns with lists into column with nested lists?

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]]


Solution 1:[1]

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]]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gustavolq