Python NumPy delete() Function

Python NumPy delete() function is used to delete the elements based on index position. And this function returns a new array with the deletion of sub-arrays along with the specified axis. For a one-dimensional array, this function returns those entries not returned by arr[obj].

In this article, I will explain how to use 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 Python NumPy delete() Function

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


# Below are the quick examples

# Example 1: Create 1-D array using arange()
arr=np.arange(10)

# Using delete() to delete the index=1
arr1=np.delete(arr,1)

# Example 2: Create an 2-D array using arange() & reshape()
arr=np.arange(12).reshape(3,4)
# Using delete() to delete the obj=1, axis=0
arr1=np.delete(arr, 1 , axis = 0)

# Example 3: Using delete() to delete the obj=2,axis=1
arr1=np.delete(arr, 2 ,  axis = 1)

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

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

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

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

# Example 8: 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()

  • arr : It is an input array from where elements are to be deleted.
  • obj : Index position or list of index positions of items to be deleted from the input array
  • axis: Axis along which we 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 we 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 we want to delete.

Delete Elements from 1 – D Array

To delete elements from 1-D array based on index position using numpy.delete() function. First we have to initialize an array using numpy.arange(). Then apply numpy.delete() function by passing index position as a object.


import numpy as np
# Create 1-D array using arange()
arr=np.arange(10)
print(arr)

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

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

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

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

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

From the above, the delete() function returned an array with the deleted element.

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

We can delete an entire column from the 2-D array using numpy.delete() function.

For example, 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

We can get the flattened array from the 2-D array using numpy.divide() along with default axis = None. For example, we 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. We 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 we 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

We 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]]

8. 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

Vijetha

With 5 of experience in technical writing, I have had the privilege to work with a diverse range of technologies like Python, Pandas, NumPy and R. During this time, I have consistently demonstrated my ability to grasp intricate technical details and transform them into comprehensible materials.

Leave a Reply

You are currently viewing Python NumPy delete() Function