The delete()
method deletes the values at specified indices.
import numpy as np
array1 = np.array([0, 1, 2, 3])
# delete at index 2
array2 = np.delete(array1, 2)
print(array2)
# Output: [0 1 3]
delete() Syntax
The syntax of delete()
is:
numpy.delete(array, obj, axis = None)
delete() Arguments
The delete()
method takes four arguments:
array
- array to delete elements fromobj
- indices at which values are deletedaxis
(optional) - the axis along which the values are deleted
Note: By default, axis
is None
, and the array is flattened.
delete() Return Value
The delete()
method returns an array with values deleted.
Example 1: Delete an Array Element at Given Index
import numpy as np
array1 = np.array([0, 1, 2, 3])
# delete values from array1 at index 2
newArray = np.delete(array1, 2)
print(newArray)
Output
[0 1 3]
Example 2: Delete an Array Element at Given Indices
We can delete different array elements at different indices.
import numpy as np
array1 = np.array([0, 1, 2, 3])
indices = [1, 2]
# delete values at indices 1 and 2
array3 = np.delete(array1, obj = indices)
print(array3)
Output
[0 3]
Example 3: Delete Element of a 2-D Array
Similar to a 1-D array, we can delete elements from a 2-D array at any index.
We can also delete an entire row or column using the axis parameter. If axis
= 0, row is deleted and if axis
= 1, column is deleted.
import numpy as np
array1 = np.array([[0, 1], [2, 3]])
# no axis, element at index 1 is deleted
array3 = np.delete(array1, 1)
print('Array after deleting element at index 1\n', array3)
# axis = 0, row 1 is deleted
array4 = np.delete(array1, 1, axis = 0)
print('\nArray after deleting row 1\n', array4)
# axis = 1, column 1 is deleted
array5 = np.delete(array1, 1, axis = 1)
print('\nArray after deleting column 1\n', array5)
Output
Array after deleting element at index 1 [0 2 3] Array after deleting row 1 [[0 1]] Array after deleting column 1 [[0] [2]]
Example 4: Delete Multiple Elements of a 2-D Array
import numpy as np
array1 = np.array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[9, 10, 11]])
# delete elements at indices 0 and 1
array3 = np.delete(array1, [0, 1])
print('\nArray after deleting elements at indices 0 and 1\n', array3)
# axis=0, delete elements of row 0 and 1
array4 = np.delete(array1, [0, 1], axis=0)
print('\nArray after deleting row 0 and 1\n', array4)
# axis=1, delete elements of column 0 and 1
array5 = np.delete(array1, [0, 1], axis=1)
print('\nArray after deleting column 0 and 1\n', array5)
Output
Array after deleting elements at indices 0 and 1 [ 2 3 4 5 6 7 8 9 10 11] Array after deleting row 0 and 1 [[ 6 7 8] [ 9 10 11]] Array after deleting column 0 and 1 [[ 2] [ 5] [ 8] [11]]
Example 5: Delete Array Based on Conditions
We can also use delete()
to eliminate the items of an array that satisfy a given condition.
import numpy as np
array1 = np.array([0, 1, 2, 3, 4, 5])
# delete elements that satisfies the condition
array5 = np.delete(array1, array1[array1%2 == 1])
print('Array after deleting odd elements \n', array5)
Output
Array after deleting odd elements [0 2 4]