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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45327069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 16:55:28  来源:igfitidea点击:

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

pythonarrayspandasnumpy

提问by Keithx

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:

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

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!

谢谢!

回答by jezrael

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

如果 reshape 操作不清楚读取,则向一维数组添加维度的更明确方法是使用 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) 

回答by Alex F

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)