HalfPintBoy - 1 year ago 332
Python Question

# 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!

Answer Source

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)
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download