numpy append-附加数组值的列而不是行

时间:2018-12-04 16:24:37

标签: python numpy append

我有一个二维的numpy数组,该数组已初始化并随后填充了一些值。我想附加一个新行,但是,我添加了新行,该行由应该附加的行的元素组成。

初始数组打印输出如下:

(6404, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2795, 750)
(6403, 10300, 1, 13, 2018, 10, 1, 195.0, 1, 420, 720, 3094, 675)
(6402, 10300, 3, 13, 2018, 10, 1, 195.0, 1, 1020, 1260, 99999, 99999)
(6401, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2802, 750)
(6400, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2867, 750)

我尝试如下添加新行:

row =  np.array([99999, 999, 0, 45, 2018, 99, 99, 60, 1, 420, 1260, 999, 920])
array = np.append(array, np.array([row], dtype=array.dtype))

输出看起来像这样:

(6404, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2795, 750) 
(6403, 10300, 1, 13, 2018, 10, 1, 195.0, 1, 420, 720, 3094, 675)
(6402, 10300, 3, 13, 2018, 10, 1, 195.0, 1, 1020, 1260, 99999, 99999)
(6401, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2802, 750)
(6400, 10300, 2, 13, 2018, 10, 1, 195.0, 1, 720, 1020, 2867, 750)
(99999, 99999, 99999, 99999, 99999, 99999, 99999, 99999.0, -97, 99999, 99999, 99999, 99999)99999)                                                    0300) 
(99999, 99999, 99999, 99999, 99999, 99999, 99999, 99999.0, -97, 99999, 99999, 99999, 99999)
(10300, 10300, 10300, 10300, 10300, 10300, 10300, 10300.0, 60, 10300, 10300, 10300, 10300) 
(0, 0, 0, 0, 0, 0, 0, 0.0, 0, 0, 0, 0, 0)
(44, 44, 44, 44, 44, 44, 44, 44.0, 44, 44, 44, 44, 44)
(2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018.0, -30, 2018, 2018, 2018, 2018)
(99, 99, 99, 99, 99, 99, 99, 99.0, 99, 99, 99, 99, 99) 
(99, 99, 99, 99, 99, 99, 99, 99.0, 99, 99, 99, 99, 99)
(60, 60, 60, 60, 60, 60, 60, 60.0, 60, 60, 60, 60, 60) 
(1, 1, 1, 1, 1, 1, 1, 1.0, 1, 1, 1, 1, 1)
(420, 420, 420, 420, 420, 420, 420, 420.0, -92, 420, 420, 420, 420)
(1260, 1260, 1260, 1260, 1260, 1260, 1260, 1260.0, -20, 1260, 1260, 1260, 1260)
(3094, 3094, 3094, 3094, 3094, 3094, 3094, 3094.0, 22, 3094, 3094, 3094, 3094) 
(1080, 1080, 1080, 1080, 1080, 1080, 1080, 1080.0, 56, 1080, 1080, 1080, 1080)

我想将该行作为一行附加,但由于我以前以相同的方式使用它,因此暂时没有看到该错误。

1 个答案:

答案 0 :(得分:0)

使用concatenate代替append

array = np.concatenate((array, row[None,:]))