6

Given

data= [
    (array([0,0,1]),1),
    (array([0,1,1]),1),
    (array([1,0,1]),0),
    (array([1,1,1]),1)    
]

How can you convert it to a Pandas DataFrame so that each column is separate?

A   B   C   Z
0   0   1   1
0   1   1   1
1   0   1   0
1   1   1   1
1
  • I'd updated my answer if you are interested. Commented May 30, 2017 at 21:13

2 Answers 2

5

I'd use np.append in a list comprehension

pd.DataFrame([np.append(*row) for row in data], columns=list('ABCZ'))

   A  B  C  Z
0  0  0  1  1
1  0  1  1  1
2  1  0  1  0
3  1  1  1  1

Or more efficiently with np.column_stack and zip

pd.DataFrame(np.column_stack(list(zip(*data))), columns=list('ABCZ'))

   A  B  C  Z
0  0  0  1  1
1  0  1  1  1
2  1  0  1  0
3  1  1  1  1

Timing

%timeit pd.DataFrame([np.append(*row) for row in data], columns=list('ABCZ'))
1000 loops, best of 3: 460 µs per loop

%timeit pd.DataFrame(np.column_stack(list(zip(*data))), columns=list('ABCZ'))
10000 loops, best of 3: 130 µs per loop

%timeit pd.DataFrame([e[0].tolist()+[e[1]] for e in data],columns=['A','B','C','Z'])
1000 loops, best of 3: 446 µs per loop
Sign up to request clarification or add additional context in comments.

1 Comment

Another one: temp1,temp2 = np.dstack(data)[0] -- pd.DataFrame(np.vstack([np.vstack(temp1).T,temp2]), columns=list('ABCZ')), but not as fast as zip
1

Convert your array to a list of lists and then put it into a Dataframe.

pd.DataFrame([e[0].tolist()+[e[1]] for e in data],columns=['A','B','C','Z'])
Out[265]: 
   A  B  C  Z
0  0  0  1  1
1  0  1  1  1
2  1  0  1  0
3  1  1  1  1

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.