簡體   English   中英

如何通過逐行添加numpy數組來創建數據框

[英]How to create Dataframe by adding row by row numpy arrays

因此,我得到了一堆要以流方式添加到數據幀的數組。 我正在嘗試如下

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])

我收到空的數據框

Out
          1         2         3          4  ...         8         9        10    y
0  4.490397 -9.633152  7.701815 -15.195824  ...  2.666405  2.962832  1.229739  6.0

[1 rows x 11 columns]
Empty DataFrame
Columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, y]
Index: []

在您的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) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM