Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 629
Hugo Avatar asked Feb 21 '14 15:02

Hugo


People also ask

Can you create a pandas series from NumPy array?

A NumPy array can be converted into a Pandas series by passing it in the pandas. Series() function.

Can you use NumPy on pandas DataFrame?

Pandas expands on NumPy by providing easy to use methods for data analysis to operate on the DataFrame and Series classes, which are built on NumPy's powerful ndarray class.

What is the difference between NumPy array and pandas DataFrame?

The essential difference is the presence of the index: while the Numpy Array has an implicitly defined integer index used to access the values, the Pandas Series has an explicitly defined index associated with the values.


1 Answers

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]})
like image 189
Alvaro Fuentes Avatar answered Sep 28 '22 03:09

Alvaro Fuentes