Numpy is a great Python library for array manipulation. You can easily calculate mathematical calculation using the Numpy Library. As a data scientist, you should know how to create, index, add and delete Numpy arrays, As it is very helpful in data preparation and cleaning process. In this section of How to, you will know how to append and insert array or its elements using the numpy append and numpy insert function. So Let’s start.
If you have not installed numpy and wants to use numpy with Pycharm then read this tutorial of How to install numpy in pycharm?
Appending the Numpy Array
Lets we want to add the list [5,6,7,8] to end of the above-defined array a. To append one array you use numpy append() method. The syntax is given below.
append(array1, array2, axis = None or )
Where type is
array1: Numpy Array, original array
array2: Numpy Array, To Append the original array.
axis: It is optional default is 0. Axis along which values are appended.
Here axis is not passed as an argument so, elements will append with the original array a, at the end.
Appending the Numpy Array using Axis
Axis tell the python interpreter to append the elements along the axis. Let’s define the two 3 -Dimensional array.
Using axis = 0 ; Note that default value of axis is 0.
You can clearly see when you append a and b with axis=0. Then the matrix b appended with an at the end of the matrix. Now the shape of the matrix is 4x3x4. You can say when the axis is 0, then append operations are done along the first dimension of the matrix. In this case, it is 2×2 that is 4.
Appending the numpy with axis =1
When you append the array using the axis =1, then append operation are done along the rows of the matrix a and b. It means all the first rows of b are appended with the first rows of a and the same for the other rows.
Appending the numpy with axis =2.
When you use axis =2, then all the append operation are done along the columns. Due to this, the column dimension changes to 2x3x8.
The np.append() method appends the entire matrix to with the original matrix. But what about you only want to insert a certain element inside the matrix. In this case, you will use the numpy insert() method.
How to insert an element inside the Numpy 3 Dimensional Array?
You can also insert an element using the Numpy insert() method along the axis. The syntax for this is the below.
insert(array,position, value, axis)
array: The array you want to insert.
position: Position where you want to insert the element.
value: The value you want to insert.
axis: For performing the row-wise or column wise operation.
The axis has the default value that is 0.
Inserting the element using axis = 0
Let’s take a 3 Dimensional array an of 2x3x4.
The insertion of the element along the axis =0, the dimension of the array changes to 3x3x4, with the same value 111 in the entire rows and columns.
Inserting the element using axis = 1
If the axis is equal to 1, then insertion operation will be done on the rows. The value 111 will insert along the position in the rows wise. It changes the dimension to 2,x4x4.
Inserting the element using axis = 2
Appending and insertion in the Numpy are different. The Numpy append method is to append one array with another array and the Numpy insert method used for insert an element. The operation along the axis is very popular for doing row wise or column wise operations.
That’s all in the section of How to on Numpy, if you want to suggest some new article, then you can contact us, We are always ready for you. In the mean time, you can subscribe us and like our Data Science Learner Page.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.