将Numpy数组按列转换为Pandas DataFrame(作为单行)

时间:2021-01-01 21:40:29

I have a numpy array looking like this:

我有一个看起来像这样的numpy数组:

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:

然后我试图将该数组转换为具有逻辑“一列一值”的pandas数据帧,如下所示:

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?

这种方法引发了ValueError:传递值的形状是(1,11),索引暗示(11,11)。我做错了什么以及如何以正确的方式执行它?

Thanks!

谢谢!

2 个解决方案

#1


9  

You need numpy.reshape:

你需要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

如果重塑操作不清楚,那么向1d数组添加维度的更明确的方法是使用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:

感谢Divakar的建议:

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:

另一个解决方案,谢谢piRSquared:

pd.DataFrame([a], [0], columns) 

#2


0  

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)

#1


9  

You need numpy.reshape:

你需要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

如果重塑操作不清楚,那么向1d数组添加维度的更明确的方法是使用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:

感谢Divakar的建议:

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:

另一个解决方案,谢谢piRSquared:

pd.DataFrame([a], [0], columns) 

#2


0  

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)