How to Append Data into Numy ndarray


Sometimes we nend append some elements to a numpy ndarray, we can do by numpy.append function.

numpy.append(arr, values, axis=None)

Notice: values must be of the correct shape (the same shape as arr, excluding axis).

Here is an examples:

import numpy as np

vec = np.array([[1,2,3],[4,5,6],[7,8,9]])
ele = np.array([[0,0,0]])

vec_1 = np.append(vec,ele,axis = 0)
print vec_1

The result is:

[[1 2 3]
 [4 5 6]
 [7 8 9]
 [0 0 0]]

If you want to append more data, you can check:

import numpy as np

vec = np.array([[1,2,3],[4,5,6],[7,8,9]])
ele = np.zeros((5,3))

vec = np.append(vec,ele,axis = 0)
print vec

The result is:

[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]

,