16

I would like to have the 3 columns of a numpy array

px[:,:,0]
px[:,:,1]
px[:,:,0]

into a pandas Dataframe.

Should I use?

df = pd.DataFrame(px, columns=['R', 'G', 'B'])

Thank you

Hugo

6
  • 1
    you have to pass DataFrame with 2D input, not a 3D array Commented Feb 21, 2014 at 15:58
  • May I slice through columns? df = pd.DataFrame((px[:,:,0],px[:,:,1],px[:,:,2]), columns=['R', 'G', 'B']) Commented Feb 21, 2014 at 16:01
  • np, what exactly do you want to do? Commented Feb 21, 2014 at 16:12
  • Could you post the content of px? Commented Feb 21, 2014 at 16:13
  • [[[ 16 47 3] [ 14 45 1] [ 6 39 0] ..., [ 40 71 39] [ 42 73 41] [ 46 77 45]] [[ 8 41 0] [ 2 35 0] [ 23 56 11] ..., [ 35 69 32] [ 38 72 37] [ 41 75 40]] [[ 6 39 0] [ 24 60 14] [ 31 67 21] Commented Feb 21, 2014 at 16:17

1 Answer 1

24

You need to reshape your array first, try this:

px2 = px.reshape((-1,3))
df = pd.DataFrame({'R':px2[:,0],'G':px2[:,1],'B':px2[:,2]})
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.