I have a DataFrame:
JavaScript
x
5
1
0 1 2
2
0 1 4 7
3
1 2 5 8
4
2 3 6 9
5
and a second DataFrame:
JavaScript
1
5
1
0 1 2
2
0 10 13 16
3
1 11 14 17
4
2 12 15 18
5
and i need this two Dataframes to become this one DataFrame:
JavaScript
1
5
1
0 1 2
2
0 (1, 10) (4, 13) (7, 16)
3
1 (2, 11) (5, 14) (8, 17)
4
2 (3, 12) (6, 15) (9, 18)
5
I need nice little tuples, all together in one frame. How is that possible?
Advertisement
Answer
Create tuples in both DataFrames and join by +
:
JavaScript
1
7
1
df = df1.applymap(lambda x: (x, )) + df2.applymap(lambda x: (x, ))
2
print (df)
3
0 1 2
4
0 (1, 10) (4, 13) (7, 16)
5
1 (2, 11) (5, 14) (8, 17)
6
2 (3, 12) (6, 15) (9, 18)
7
Or join by concat
and aggregate by index tuple
:
JavaScript
1
7
1
df = pd.concat([df1, df2]).groupby(level=0).agg(tuple)
2
print (df)
3
0 1 2
4
0 (1, 10) (4, 13) (7, 16)
5
1 (2, 11) (5, 14) (8, 17)
6
2 (3, 12) (6, 15) (9, 18)
7