Working with numpy.delete() function (4 examples)

Updated: February 29, 2024 By: Guest Contributor Post a comment

Introduction

The numpy.delete() function is a versatile tool in the NumPy library, allowing users to delete elements from a NumPy array along specified axes. This tutorial will explore how to use numpy.delete() with four progressively complex examples, helping you grasp the function’s utility and flexibility. Whether you’re dealing with 1D, 2D, or higher-dimensional arrays, understanding numpy.delete() can significantly enhance your data manipulation capabilities in Python.

Understanding numpy.delete()

Before diving into examples, it’s important to understand the syntax of numpy.delete():

numpy.delete(arr, obj, axis=None)

Where:

  • arr: The input array from which elements are to be deleted.
  • obj: Indicates which sub-arrays to remove. Can be a slice, integer or array of integers.
  • axis: The axis along which to delete the given sub-array. If unspecified (None), the array will be flattened before deletion.

Example 1: Deleting an Element from a 1D Array

Let’s start with the basics – removing an element from a one-dimensional array.

import numpy as np

# Creating a one-dimensional array
arr = np.array([1, 2, 3, 4, 5])

# Deleting the second element
new_arr = np.delete(arr, 1)

# Output
print(new_arr)

The output will be:

[1, 3, 4, 5]

In this example, we successfully removed the second element from our array. Note that indices in Python start from zero.

Example 2: Deleting Multiple Elements from a 1D Array

Next, let’s see how to delete multiple elements at once by passing an array of indices.

import numpy as np

# Creating a one-dimensional array
arr = np.array([10, 20, 30, 40, 50, 60])

# Deleting elements at indexes 1, 3, and 4
new_arr = np.delete(arr, [1, 3, 4])

# Output
print(new_arr)

The output will be:

[10, 30, 60]

This method allows for the removal of multiple, non-consecutive elements from the array.

Example 3: Deleting an Entire Row from a 2D Array

Moving to two-dimensional arrays, you might occasionally need to delete an entire row. This can be achieved by specifying the row index and the axis.

import numpy as np

# Creating a two-dimensional array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Deleting the second row
new_arr = np.delete(arr, 1, axis=0)

# Output
print(new_arr)

The output will be:

[[1, 2, 3],
 [7, 8, 9]]

You can see that the entire second row has been removed, altering the structure of our 2D array.

Example 4: Deleting Specific Columns Across Multiple Rows

For more advanced manipulation, deleting specific columns from a 2D array demonstrates the power of numpy.delete() in handling complex array structures.

import numpy as np

# Creating a two-dimensional array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# Deleting the first and third columns
new_arr = np.delete(arr, [0, 2], axis=1)

# Output
print(new_arr)

The output will display only the second column across all rows, proving that numpy.delete() can precisely control which elements to remove, irrespective of the array’s dimensions.

[[2],
 [5],
 [8]]

Conclusion

Through these examples, we’ve covered the fundamental to advanced uses of the numpy.delete() function. Starting from simple element removals to complex operations across multiple dimensions, numpy.delete() serves as a powerful tool for array manipulation in Python. Mastering its application can significantly optimize your data processing and analysis workflows.