• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:19 mins read
You are currently viewing Python NumPy delete() Function

Python NumPy delete() function is used to delete elements based on index positions, and it returns a new array with the specified elements removed. For a one-dimensional array, this function returns those entries not returned by arr[obj].

Advertisements

In this article, I will explain how to use the NumPy delete() function to return a new array with the specified subarray deleted from the input array. If the axis parameter is not used then the input array is flattened.

1. Quick Examples of NumPy delete() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy delete() function.


# Quick examples of numpy delete() function

# Example 1: Using delete() 
# To delete the index=2
arr=np.arange(10)
arr1=np.delete(arr,2)

# Example 2: Using list of values
arr1=np.delete(arr,[4,1])

# Example 3: Using delete()
# To delete the obj=1, axis=0
arr=np.arange(12).reshape(3,4)
arr1=np.delete(arr, 1 , axis = 0)

# Example 4: Using delete() function
# To delete the obj=2,axis=1
arr1=np.delete(arr, 2 ,  axis = 1)

# Example 5: Using delete() function
# To delete the index=1
arr1=np.delete(arr, 0,axis = None )

# Example 6: Delete multiple rows
arr1=np.delete(arr,[0,1,2],axis=0)

# Example 7: Delete multiple columns
arr1=np.delete(arr,[0,1,2],axis=1)  

# Example 8: Delete multiple columns
# Using slice operation
arr1 = np.delete(arr, slice(2), 1)

# Example 9: Delete multiple rows 
# Using numpy.s_[]
arr1 = np.delete(arr, np.s_[:2],axis= 0)

2. Syntax of NumPy delete()

Following is the syntax of the numpy.delete() function.


# Syntax of numpy.delete() 
numpy.delete(arr, obj, axis=None)

2.1 Parameters of delete()

Following are the Parameters delete()

  • arr : The input array from which elements will be deleted.
  • obj : Index position or list of index positions of items to be deleted from the input array.
  • axis: Axis along which you want to delete. If it is '1' then delete columns, or '0' then delete rows. If the axis is None then return the flattened array.

2.2 Return value of delete()

Returns a new array with the deletion of sub-arrays along with the specified axis. If the axis is None then return the flattened array.

3. Usage of NumPy delete() Function

Using NumPy delete() function you can delete elements from the NumPy array and delete specified rows and columns from the 2-D array along with the specified axis. It returns a new array with sub-arrays along an axis deleted. For a 1D array, it just deletes the object which you want to delete.

The numpy.delete() function is used to delete specified elements from an array along a specified axis. It returns a new array with the specified elements removed.

3.1 Deleting an Element from a 1D Array

You can use the numpy.delete() function to remove an element from a 1D NumPy array. First, you have to initialize an array using numpy.arange(10), which generates an array of integers from 0 to 9.

The np.delete() function is used to delete the element at index 2 from the original array (arr). The result is stored in the variable arr1, and the modified array is printed. The element at index 2 (value 2) has been removed, and the resulting array is stored in arr1. The original array arr remains unchanged.


# Import numpy
import numpy as np

# Create an array
arr = np.arange(10)
print("Original array:\n", arr)

# Using delete() function
# Delete element at index 2
arr1 = np.delete(arr, 2)
print("Array after deleting element at index 2:\n",arr1)

Yields below output.

numpy delete

3.2 Deleting Multiple Elements from a 1D Array

You can also use the numpy.delete() function to delete multiple elements from a 1D NumPy array. First, you have to initialize an array using numpy.arange(10), which generates an array of integers from 0 to 9.

Here, the np.delete() function is used to delete elements at indices 4 and 1 from the original array arr. The resulting array is stored in arr1, and both the original and modified arrays are printed.


# Using list of values
arr1=np.delete(arr,[4,1])
print("Array after deleting elements at indices 4 and 1:\n",arr1)

Yields below output.

numpy delete

It shows that the elements at indices 4 and 1 have been successfully removed from the original array, resulting in the modified array arr1.

4. Delete Row From 2-D Array along Axis = 0

Using numpy.delete() function, you can delete any row and column from the 2-D NumPy array along with the specified axis, for that we have to initialize the 2-D NumPy array using numpy.arange() and get the reshape using numpy.reshape().

For example, to delete the second row, use obj=1,axis=0 as arguments to numpy.delete() function. The original array remains unchanged, and a new copy of the NumPy array is returned.


# Create an 2-D array using arange() & reshape()
arr=np.arange(12).reshape(3,4)
print(arr)

# Output:
# [[ 0  1  2  3]
# [ 4  5  6  7]
# [ 8  9 10 11]]

# Using delete() to delete the 2nd row
arr1=np.delete(arr, 1 , axis = 0)
print(arr1)

# Output :
# [[ 0  1  2  3]
# [ 8  9 10 11]]

# Using delete() to delete the 3rd row
arr1=np.delete(arr, 2 , axis = 0)
print(arr1)

# Output :
# [[0 1 2 3]
# [4 5 6 7]]

5. Delete Column From 2-D Arrays along Axis = 1

You can delete an entire column from the 2-D array using numpy.delete() function. For instance, to delete the third column, use obj=2, axis=1 argument to numpy.delete() function. The original array remains unchanged, and a new copy of the NumPy array is returned.


# Using delete() to delete 2nd column
arr1=np.delete(arr, 2 ,  axis = 1)
print(arr1)

# Output :
# [[ 0  1  3]
# [ 4  5  7]
# [ 8  9 11]]

# Using delete() to delete 1st column
arr1=np.delete(arr, 0 ,  axis = 1)
print(arr1)

# Output :
# [[ 1  2  3]
# [ 5  6  7]
# [ 9 10 11]]

6. Delete Elements From 2-D Arrays along Axis = None

You can get the flattened array from the 2-D array using numpy.divide() along with default axis=None. For instance, you want to remove the ‘0’ based index as an object along with the default axis using numpy.divide().


# Using delete() to delete the index=1
arr1=np.delete(arr, 0,axis = None )
print(arr1)

# Output :
# [ 1  2  3  4  5  6  7  8  9 10 11]

7. Delete Multiple Rows and Columns

Multiple rows and columns can be removed at once by specifying the list or a slice in the second parameter obj. You can delete multiple rows and columns at once using the following ways.

  • Using list
  • By using slicing
  • numpy.s_[] Function

7.1 Using List to Delete Multiple Rows & Columns

Using the list of values as an object parameter of numpy.divide() function you can delete Multiple columns or multiple rows at a time along with a specified axis. If you use the default axis it will give a flattened array.


# Delete multiple rows
arr1=np.delete(arr,[0,1,2],axis=0)
print(arr1)

# Output:
# [[ 8  9 10 11]]

# Delete multiple columns
arr1=np.delete(arr,[0,1,2],axis=1)
print(arr1)

# Output:
# [[ 3]
#  [ 7]
# [11]]

# Use numpy.delete() along axis = None
arr1 = np.delete(arr, [1, 2], None)
print(arr1)

# Output:
# [0  3  4  5  6  7  8  9 10 11]

7.2 Use Slicing Operation to Delete Multiple Rows & Columns

It is also possible to specify the multiple rows and columns by using the slice specifying a range with [start: stop: step]. Create a slice object with a slice() and specify it as a second parameter obj.

It is equivalent to [: stop] if there is a single argument [start:stop] if there are two arguments, and [start: stop: step] if there are three arguments. If you want to omit, specify None explicitly.


# Delete multiple columns using slice operation
arr1 = np.delete(arr, slice(2), 1)
print(arr1)

# Output:
# [[ 2  3]
# [ 6  7]
# [10 11]]

7.3. Use numpy.s_[] Function

You can also delete multiple columns and multiple rows along with specified axis using numpy.s_[]. Let’s remove the first and second columns along with axis=0 using np.s_[] function, this function you can write in a form [start:stop:step]. Remember array index starts from 0.


# Delete multiple rows using numpy.s_[]
arr1 = np.delete(arr, np.s_[:2],axis= 0)
print(arr1)

# Output:
# [[ 8  9 10 11]]

Frequently Asked Questions

What does numpy.delete() do?

The numpy.delete() function in NumPy is used to delete specified elements along a particular axis from an array. It returns a new array with the specified elements removed, without modifying the original array.

Can I delete elements from a multi-dimensional array using numpy.delete()?

You can use numpy.delete() to delete elements from a multi-dimensional array. When working with multi-dimensional arrays, you need to specify the axis along which the deletion should occur.

How do I use numpy.delete() to remove a specific element from an array?

To use numpy.delete() to remove a specific element from an array, you need to provide the array, the index of the element you want to delete, and optionally the axis along which the deletion should occur.

Does numpy.delete() modify the original array?

The numpy.delete() function does not modify the original array. Instead, it returns a new array with the specified elements removed. This behavior is consistent with the general philosophy of NumPy, which aims to avoid modifying arrays in place to prevent unintended side effects.

How do I delete an entire row or column from a 2D array?

To delete an entire row or column from a 2D array using numpy.delete(), you can specify the index of the row or column along with the appropriate axis parameter. Here are examples for deleting a row and a column

What happens if I set axis=None in numpy.delete()?

If you set axis=None in the numpy.delete() function, it means that the array will be flattened before deletion. This implies that all elements specified in the obj parameter will be removed from the flattened version of the array.

Conclusion

In this article, I have explained how to use numpy.delete() function that returns a new array with the specified subarray deleted from the input array with examples.

Happy Learning!!

References