Numpy is a python module that allows you to create a numpy array and manipulate them easily using various functions. Do you want to remove the item from the numpy array? If yes then this post is for you. In this tutorial, you will know how to remove items from the numpy array in through steps.
How to Remove item from numpy array ( Steps )
In this section, you will know all the steps that you will follow to remove a particular item from the numpy array. I will advise you to follow the same steps for better understanding. In addition, all these coding works are done on Google Colab. Let me know all the steps.
Step 1: Import the required package
The first step is to import all the necessary libraries that will be used in the example. I am using the numpy package only so let’s import it using the import statement,
import numpy as np
Step 2: Create a sample numpy array
The next step is to create a sample numpy array that will be used for implementing the example. You can easily create a 1D numpy array using the numpy.array() function.
Use the below line of code to create a dummy array.
numpy_array = np.array([10,20,30,40,50])
Step 3: Use the numpy.delete() function
Now the main part is to remove the item from the numpy array. To do so you will use the np.delete() function. It accepts the two main parameters. One is your input numpy array and the other is the item index or items indices.
Add the below lines of code to remove the item.
final_array = np.delete(numpy_array,)
Step 4: Display the numpy array
Now the last step is to check whether the item has been deleted or not. For this, you have to use the print(your_numpy_array) to display all the items.
Execute the entire code to display the final numpy array items.
import numpy as np numpy_array = np.array([10,20,30,40,50]) final_array = np.delete(numpy_array,) print(final_array)
You can also delete more than one item from the numpy array. Just pass the list of indices of the items you want to delete.
final_array = np.delete(numpy_array,[2,4])
You can see the output below when you will run the below lines of code.
import numpy as np numpy_array = np.array([10,20,30,40,50]) final_array = np.delete(numpy_array,[2,4]) print(final_array)
The Numpy package is very useful for data manipulation. Doing mathematical calculations becomes very easy when you convert data to a numpy array. If you want to delete the items from the numpy array then the above steps will achieve that.
I hope you have liked this tutorial. If you have any doubts then you can contact us for more help.
Join our list
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.