Edit multidimensional data
Remove selected columns or rows from a matrix while preserving the other dimension.
NumPy · Function
Returns a new array with selected elements or sub-arrays removed. Use axis to control the dimension, or leave it as None to delete from the flattened input.
np.delete returns a new array after removing the positions selected by obj. With axis specified, it removes sub-arrays along that dimension; with axis=None, it first treats the input as flattened and returns a one-dimensional result.
numpy.delete(arr, obj, axis=None)| Name | Type | Description |
|---|---|---|
arr | array_like | Input array. |
obj | slice, int, array-like of ints or bools | Indices or sub-arrays to remove along the specified axis. |
axis | int, optional | Axis along which to delete the subarray specified by obj. If None, obj applies to the flattened array. |
out ndarray: A copy of arr with the elements specified by obj removed.
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = np.delete(arr, 1, axis=1)
print(result)
# [[1 3]
# [4 6]]Creates a new array after removing column 1; the input remains unchanged.
Remove selected columns or rows from a matrix while preserving the other dimension.
Drop individual entries or ranges from a one-dimensional array.
Delete positions selected by integer indices or a boolean mask.
It returns a new array with the selected elements or sub-arrays removed, rather than editing arr in place. For a one-dimensional input, the result contains entries not selected by arr[obj].
Follow-up: What happens when axis is omitted? obj is applied after flattening the input, and the returned result is one-dimensional.
obj may be a slice, an integer, an integer array, or a boolean array. These identify the indices or positions to remove along the chosen axis.
Follow-up: How are boolean arrays interpreted? They act as masks for selecting positions to delete, rather than being converted to integer indices.
axis selects the dimension whose sub-arrays are removed. If it is None, NumPy applies obj to the flattened input instead.
Follow-up: Does np.delete mutate the original array? No; it always produces a copy, so the original must be replaced explicitly if desired.
numpy.appendnumpy.insertnumpy.placenumpy.take