15

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!

0

2 Answers 2

24

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

Shorter one : pd.DataFrame(a[None],columns=columns).
pd.DataFrame([a], [0], columns)
1

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)

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.