Learn numpy.append() method with examples
NumPy is an open-source library available in Python that is used to perform numerical processing on arrays. Suppose we want to add elements at the end of an array or even combine two arrays together using NumPy. Therefore, in this tutorial, we will learn how to use numpy.append()
method along with a few examples in Python.
Syntax of numpy.append()
Let us first understand the syntax of numpy.append()
mentioned below before we use it in examples.
numpy.append(array,values,axis)
In the above syntax,
- array: Numpy array where we want to append values.
- Values: values that are to be appended to the array which themselves are in form of an array
- axis: It can take values 1 and 0. It represents the axis along which the values are to be appended.
Note:
- If the axis is given as None, then the arrays will be flattened together containing only a single dimension
- The values are appended to the copy of the array and not the original array
Appending values to the array with axis as None
Let us initially append values to an array with the axis as None. In this case, the original array and value arrays are flattened together. This means that they are merged together with only a single dimension.
In the below-mentioned example, initially, we import the NumPy library. Then we have the first array which is a 2X2 dimension array. We want to append values in 2X2 dimensional format to this array. Since the axis is not given so it will simply append values and give a resultant array of a single dimension.
import numpy as np arr1 = np.array([[10, 20], [30, 40]]) arr2=np.array([[1,2],[3,4]]) result = np.append(arr1, arr2) print(result)
Output:
[10 20 30 40 1 2 3 4]
Appending values to the array with axis=0
Now we will keep the axis value as 0. This will append the values along the horizontal axis at the end of the array. Thus finally we get a resultant array of dimension 4X2.
import numpy as np arr1 = np.array([[10, 20], [30, 40]]) arr2=np.array([[1,2],[3,4]]) result = np.append(arr1, arr2,axis=0) print(result)
Output:
[[10 20] [30 40] [ 1 2] [ 3 4]]
Appending values to the array with axis=1
Now we will keep the axis value as 1. This will append the values along the vertical axis at the end of the array. Thus finally we get a resultant array of dimensions 2X4.
import numpy as np arr1 = np.array([[10, 20], [30, 40]]) arr2=np.array([[1,2],[3,4]]) result = np.append(arr1, arr2,axis=1) print(result)
Output:
[[10 20 1 2] [30 40 3 4]]
Appending values to the array of different shape
In this case, we have the original array having dimensions of 1X2. We want to append values to this array having dimensions of 2X2. We will append these values along the horizontal axis by keeping the axis value as 0. Thus we get a resultant array after appending the dimensions of 3X2.
import numpy as np arr1 = np.array([[10, 20,]]) arr2=np.array([[1,2],[3,4]]) result = np.append(arr1, arr2,axis=0) print(result)
[[10 20] [ 1 2] [ 3 4]]
Thus we have reached the end of this tutorial on how to use the NumPy append() method along with a few examples. To learn more about NumPy array click on the following link: Access a NumPy array by column in Python
Leave a Reply