Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Numpy array to Pandas DataFrame column-wise (As Single Row)

I have a numpy array looking like this:

a = np.array([35,2,160,56,120,80,1,1,0,0,1])

Then I'm trying to transform that array into pandas dataframe with logic "one column-one value" like this:

columns=['age','gender','height',
     'weight','ap_hi','ap_lo',
     'cholesterol','gluc','smoke',
     'alco','active']

values = a

df = pd.DataFrame(a,columns=columns)

This approach raises ValueError: Shape of passed values is (1, 11), indices imply (11, 11). What am I doing wrong and how to perform it in a right way?

Thanks!

like image 584
Keithx Avatar asked Jul 26 '17 12:07

Keithx


People also ask

How do I turn an array into a column?

Conversion of an Array into a Column Vector. This conversion can be done using a(:) operation. A(:) reshapes all elements of A into a single column vector. This has no effect if A is already a column vector.

How do I convert columns to rows in pandas?

Method #2: Using pivot() method. In order to convert a column to row name/index in dataframe, Pandas has a built-in function Pivot. Now, let's say we want Result to be the rows/index, and columns be name in our dataframe, to achieve this pandas has provided a method called Pivot.

How do you convert rows into columns and columns into rows in pandas?

Pandas DataFrame: transpose() function The transpose() function is used to transpose index and columns. Reflect the DataFrame over its main diagonal by writing rows as columns and vice-versa. If True, the underlying data is copied. Otherwise (default), no copy is made if possible.


2 Answers

You need numpy.reshape:

columns=['age','gender','height',
     'weight','ap_hi','ap_lo',
     'cholesterol','gluc','smoke',
     'alco','active']

a = np.array([35,2,160,56,120,80,1,1,0,0,1])

df = pd.DataFrame(a.reshape(-1, len(a)),columns=columns)
print (df)
   age  gender  height  weight  ap_hi  ap_lo  cholesterol  gluc  smoke  alco  \
0   35       2     160      56    120     80            1     1      0     0   

   active  
0       1  

If the reshape operation is not clear to read, a more explicit way of adding a dimension to the 1d array is to use numpy.atleast_2d

pd.DataFrame(np.atleast_2d(a), columns=columns)

Or simplier add [] (but slower if really many columns):

df = pd.DataFrame([a],columns=columns)
print (df)
   age  gender  height  weight  ap_hi  ap_lo  cholesterol  gluc  smoke  alco  \
0   35       2     160      56    120     80            1     1      0     0   

   active  
0       1  

Thanks Divakar for suggestion:

df = pd.DataFrame(a[None],columns=columns)
print (df)
   age  gender  height  weight  ap_hi  ap_lo  cholesterol  gluc  smoke  alco  \
0   35       2     160      56    120     80            1     1      0     0   

   active  
0       1  

And another solution, thanks piRSquared:

pd.DataFrame([a], [0], columns) 
like image 96
jezrael Avatar answered Oct 27 '22 10:10

jezrael


Just reshape the array to what you need for the dataframe.

import pandas as pd 
import numpy as np

a = np.array([35,2,160,56,120,80,1,1,0,0,1])

columns=['age','gender','height',
 'weight','ap_hi','ap_lo',
 'cholesterol','gluc','smoke',
 'alco','active']

df = pd.DataFrame(np.reshape(a, (1,len(a))),columns=columns)
like image 35
Alex F Avatar answered Oct 27 '22 11:10

Alex F