In this article we will learn about NumPy Delete functions, we will explore their usage, benefits, and best practices, along with examples.
Deletion involves removing elements or entire sections from an array, thereby reducing its size. It facilitates data cleanup, filtering, and optimization of array structures.
NumPy Delete
np.delete()
: np.delete()
function removes specified elements or slices from an array along a given axis.arr = np.array([1, 2, 3, 4, 5])
deleted_arr = np.delete(arr, [1, 3])
print(deleted_arr)
np.delete()
function to delete the elements at indices [1, 3]
from the array arr
.deleted_arr
contains the elements of arr
with the elements at indices [1, 3]
removed.[1 3 5]
deleted_arr
after the deletion operation. The elements at indices [1, 3]
(i.e., 2
and 4
) have been removed from the original array arr
.arr = np.array([[1, 2, 3], [4, 5,6],[7,8,9]])
new_arr = np.delete(arr, 1, axis = 0)
print(new_arr)
np.delete()
function to delete the row at index 1
along axis 0
(rows) from the array arr
.[[1 2 3]
[7 8 9]]
new_arr
after the deletion operation. The row at index 1
(i.e., [4, 5, 6]
) has been removed from the original array arr
.arr = np.array([[1, 2, 3], [4, 5,6],[7,8,9]])
new_arr = np.delete(arr, 1, axis = 1)
print(new_arr)
np.delete()
function to delete the column at index 1
along axis 1
(columns) from the array arr
.[[1 3]
[4 6]
[7 9]]
new_arr
after the deletion operation. The column at index 1
(i.e., [2, 5, 8]
) has been removed from the original array arr
.In conclusion, the numpy.delete()
function offers a versatile solution for manipulating NumPy arrays, enabling users to efficiently remove specific elements or sub-arrays along designated axes. By mastering its syntax and capabilities, individuals can effectively manage and transform their data arrays, enhancing their data analysis workflows. As you explore the diverse applications of numpy.delete()
, consider sharing your newfound knowledge with others in the data science community. Sharing educational articles or tutorials on platforms like blogs, forums, or social media can contribute to the collective learning experience and foster collaboration among peers. Together, we can continue to expand our understanding of NumPy and its powerful array manipulation capabilities.
We hope that you liked our information, if you liked our information, then you must share it with your friends, family and group. So that they can also get this information.
Reference: np.delete()
Also Read:
Hi, I am Vishal Jaiswal, I have about a decade of experience of working in MNCs like Genpact, Savista, Ingenious. Currently i am working in EXL as a senior quality analyst. Using my writing skills i want to share the experience i have gained and help as many as i can.
Welcome to Day 13 of Learning Python for Data Science! Today, we’re focusing on three…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…
Welcome to Day 12 of Learning Python for Data Science. Today, we’ll dive into Pandas,…
NumPy Array in Python is a powerful library for numerical computing in Python. It provides…
Welcome to Day 9 of Learning Python for Data Science. Today we will explore comprehensions,…
Test your understanding of Python Data Structure, which we learned in our previous lesson of…