The numpy.append() function is available in NumPy package. As the name suggests, append means adding something. The numpy.append() function is used to add or append new values to an existing numpy array. This function adds the new values at the end of the array.
The numpy append() function is used to merge two arrays. It returns a new array, and the original array remains unchanged.
numpy.append(arr, values, axis=None)
There are the following parameters of the append() function:
1) arr: array_like
This is a ndarray. The new values are appended to a copy of this array. This parameter is required and plays an important role in numpy.append() function.
2) values: array_like
This parameter defines the values which are appended to a copy of a ndarray. One thing is to be noticed here that these values must be of the correct shape as the original ndarray, excluding the axis. If the axis is not defined, then the values can be in any shape and will flatten before use.
3) axis: int(optional)
This parameter defines the axis along which values are appended. When the axis is not given to them, both ndarray and values are flattened before use.
This function returns a copy of ndarray with values appended to the axis.
import numpy as np a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]]) c=np.append(a,b) c
Output:
In the above code
In the output, values of both arrays, i.e., 'a' and 'b', have been shown in the flattened form, and the original array remained same.
import numpy as np a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]]) c=np.append(a,b,axis=0) c
In the above code
In the output, values of both arrays, i.e., 'a' and 'b', have been shown vertically in a single array, and the original array remained the same.
Output:
import numpy as np a=np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) b=np.array([[11, 21, 31], [42, 52, 62], [73, 83, 93]]) c=np.append(a,b,axis=1) c
Output: