Let’s see how to append to an empty array in the Numpy Python module.
A little reminder. We already discussed how to create an empty array in Numpy. We used the following code:
import numpy as np empty_array = np.empty((3, 3)) print(empty_array)
How to append to an empty array?
To append to an empty array, just use the append Numpy function.
import numpy as np empty_array = np.empty((3, 3)) new_array = np.append(empty_array, 6) print(new_array)
Using the append method, I’ve added six.
To append the empty array more and add more values, just use parentheses.
import numpy as np empty_array = np.empty((3, 3)) new_array = np.append(empty_array, (6, 3)) print(new_array)
This is what your array looks like. If you don’t like what you see, you can reshape it now.
import numpy as np empty_array = np.empty((3, 3)) new_array = np.append(empty_array, (6, 3, 4)) reshape = new_array.reshape(4, 3) print(reshape)
How to append to an empty array from another array?
Going further, you can also append an empty array with the values of another Numpy array. Python code would be like that:
import numpy as np empty_array = np.empty((3, 3)) values = [1, 2, 3] new_array = np.append(empty_array, np.array([values])) print(new_array)
As an output, an empty array has been appended by 1,2,3 values as expected.
[0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 2. 3.]