你需要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
如果 reshape 操作不清楚阅读,向一维数组添加维度的更明确的方法是使用numpy.atleast_2d
pd.DataFrame(np.atleast_2d(a), columns=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
感谢 Divakar 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
还有一个解决方案,谢谢piRSquared:
pd.DataFrame([a], [0], columns)