Reputation: 613
Assume I have two dataframes:
x1 x2 x3
y1 a1 a1 a1
y2 b1 b1 b1
y3 c1 c1 c1
x1 x2 x3
y1 a2 a2 a2
y2 b2 b2 b2
y3 c2 c2 c2
Which is the easiest way to get the following dataframe with each element a list of the two values from the same place in original dataframes?
x1 x2 x3
y1 [a1,a2] [a1,a2] [a1,a2]
y2 [b1,b2] [b1,b2] [b1,b2]
y3 [c1,c2] [c1,c2] [c1,c2]
Upvotes: 1
Views: 182
Reputation: 76947
Using applymap
In [953]: df1.applymap(lambda x: [x]) + df2.applymap(lambda x: [x])
Out[953]:
x1 x2 x3
y1 [a1, a2] [a1, a2] [a1, a2]
y2 [b1, b2] [b1, b2] [b1, b2]
y3 [c1, c2] [c1, c2] [c1, c2]
Upvotes: 3
Reputation: 210882
One (out of many) possible solution:
In [63]: d1.astype(str).add(',').add(d2.astype(str)).apply(lambda x: x.str.split(','))
Out[63]:
x1 x2 x3
y1 [a1, a2] [a1, a2] [a1, a2]
y2 [b1, b2] [b1, b2] [b1, b2]
y3 [c1, c2] [c1, c2] [c1, c2]
Upvotes: 3