The delete() method deletes the values at specified indices.
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
Output
[0 1 3]
Example 2: Delete an Array Element at Given Indices
We can delete different array elements at different indices.
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.
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
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.
Output
Array after deleting odd elements [0 2 4]