如何通过逐行添加numpy数组来创建数据框

时间:2019-06-01 01:01:04

标签: python pandas

因此,我以流式方式获取一堆数组,我想将其附加到数据框。我正在尝试如下操作

catch

我得到这样的输出和错误

df = pd.DataFrame(columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y'])

outputs = get_outputs()

for xp, yp in zip(outputs, labels):
     ex = np.append(xp, yp)
     print(ex)
     print(ex.shape)
     #trying here to create row-dataframe
     exdf = pd.DataFrame(ex, columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y']) 
     #want to append the row to the main dataframe
     df.append(exdf) 

我应该怎么做?

编辑: 合并答案后

[  4.49039745  -9.63315201   7.70181465 -15.19582367  12.6580925
  -1.17788887  -5.21339655   2.6664052    2.96283174   1.22973883
   6.        ]
(11,)
...
ValueError: Shape of passed values is (11, 1), indices imply (11, 11)

我收到空的数据框

exdf = pd.DataFrame([ex], columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y'])
print(exdf)
df.append(exdf)
print(df[:1])

1 个答案:

答案 0 :(得分:1)

在您的for循环中添加[]

 for xp, yp in zip(outputs, labels):
 ex = np.append(xp, yp)
 print(ex)
 print(ex.shape)
 #trying here to create row-dataframe
 exdf = pd.DataFrame([ex], columns=['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'y']) 
 #want to append the row to the main dataframe
 df=df.append(exdf)