1

I have a DataFrame:

      0  1  2
   0  1  4  7
   1  2  5  8
   2  3  6  9

and a second DataFrame:

        0   1   2
    0  10  13  16
    1  11  14  17
    2  12  15  18

and i need this two Dataframes to become this one DataFrame:

         0        1        2
     0  (1, 10)  (4, 13)  (7, 16)
     1  (2, 11)  (5, 14)  (8, 17)
     2  (3, 12)  (6, 15)  (9, 18)

I need nice little tuples, all together in one frame. How is that possible?

1 Answer 1

4

Create tuples in both DataFrames and join by +:

df = df1.applymap(lambda x: (x, )) + df2.applymap(lambda x: (x, ))
print (df)
         0        1        2
0  (1, 10)  (4, 13)  (7, 16)
1  (2, 11)  (5, 14)  (8, 17)
2  (3, 12)  (6, 15)  (9, 18)

Or join by concat and aggregate by index tuple:

df = pd.concat([df1, df2]).groupby(level=0).agg(tuple)
print (df)
         0        1        2
0  (1, 10)  (4, 13)  (7, 16)
1  (2, 11)  (5, 14)  (8, 17)
2  (3, 12)  (6, 15)  (9, 18)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.