5

i need to create a dataframe containing tuples from a series of dataframes arrays. What I need is the following:

I have dataframes a and b:

a = pd.DataFrame(np.array([[1, 2],[3, 4]]), columns=['one', 'two'])
b = pd.DataFrame(np.array([[5, 6],[7, 8]]), columns=['one', 'two'])

a:
   one  two
0    1    2
1    3    4

b: 
   one  two
0    5    6
1    7    8

I want to create a dataframe a_b in which each element is a tuple formed from the corresponding elements in a and b, i.e.

a_b = pd.DataFrame([[(1, 5), (2, 6)],[(3, 7), (4, 8)]], columns=['one', 'two'])

a_b: 
      one     two
0  (1, 5)  (2, 6)
1  (3, 7)  (4, 8)

Ideally i would like to do this with an arbitrary number of dataframes. I was hoping there was a more elegant way than using a for cycle I'm using python 3

2 Answers 2

4

you can use numpy.rec.fromarrays((a.values, b.values)).tolist():

In [34]: pd.DataFrame(np.rec.fromarrays((a.values, b.values)).tolist(), 
                      columns=a.columns,
                      index=a.index)
Out[34]:
      one     two
0  (1, 5)  (2, 6)
1  (3, 7)  (4, 8)

merging three DF's:

In [36]: pd.DataFrame(np.rec.fromarrays((a.values, b.values, a.values)).tolist(),
                      columns=a.columns,
                      index=a.index)
Out[36]:
         one        two
0  (1, 5, 1)  (2, 6, 2)
1  (3, 7, 3)  (4, 8, 4)

UPDATE:

suppose you don't know in advance the number of dataframes, how would you do?

In [60]: dfs = [a,b,a]

In [62]: tuple_of_dfs = (x.values for x in dfs)

In [63]: pd.DataFrame(np.rec.fromarrays(tuple_of_dfs).tolist(), columns=a.columns, index=a.index)
Out[63]:
         one        two
0  (1, 5, 1)  (2, 6, 2)
1  (3, 7, 3)  (4, 8, 4)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this works great, but suppose you don't know in advance the number of dataframes, how would you do?
@gionni, please see UPDATE section
2

You could use zip over columns of a, b

In [31]: pd.DataFrame({x: zip(a[x], b[x]) for x in a.columns})
Out[31]:
      one     two
0  (1, 5)  (2, 6)
1  (3, 7)  (4, 8)

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.