Hugo
Hugo

Reputation: 1698

How do I convert a numpy array into a pandas dataframe?

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

Upvotes: 16

Views: 57408

Answers (1)

Alvaro Fuentes
Alvaro Fuentes

Reputation: 17485

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]})

Upvotes: 24

Related Questions