HOWTO · NumPy

How to Delete Row in NumPy

Learn how to delete rows from a NumPy array by index or condition. See how numpy.delete() uses axes and returns a new array, with examples for one row, multiple rows, and boolean filtering.

On this page

NumPy arrays do not have a row-removal operation that changes their size in place. To create an array without selected rows, use numpy.delete() with the row axis, or use a Boolean mask to keep rows that meet a condition. Array indices start at 0, so index 1 means the second row.

Delete one row by index with numpy.delete()

Pass the array, the row index, and axis=0 to numpy.delete(). Axis 0 selects rows; axis 1 would select columns in a two-dimensional array. This example removes the second row:

import numpy as np

array = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9]])
without_second = np.delete(array, 1, axis=0)
print(without_second)

The returned array contains the first and third rows. numpy.delete() creates a new array and leaves array unchanged. Use the returned value in later calculations; assigning it to a new variable, as above, keeps the original available.

Delete multiple rows by index

To remove several known rows in one call, pass their indices as a sequence. Indices still refer to the original array, so removing index 1 does not change the meaning of index 3 within the same call. Here the second and fourth rows are selected for removal:

import numpy as np

array = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9],
                  [10, 11, 12]])
without_selected = np.delete(array, [1, 3], axis=0)
print(without_selected)

The remaining rows are the original rows at indices 0 and 2. If the rows to remove are determined by data values, a Boolean mask is often clearer than first collecting their indices.

Delete rows that match a condition

A Boolean mask can select which rows to keep. For a two-dimensional array, array[:, 0] selects the first column; comparing it with 5 produces one Boolean value per row. The following example retains rows whose first-column value is greater than 5:

import numpy as np

array = np.array([[1, 2, 3],
                  [4, 5, 6],
                  [7, 8, 9],
                  [10, 11, 12]])
keep = array[:, 0] > 5
filtered = array[keep]
print(filtered)

This is filtering by keeping matching rows, rather than deleting rows with numpy.delete(). To remove rows that meet a condition, invert the mask with ~, as in array[~keep]. Choose the comparison to match the column and rule in your own data.

Understand axes, indices, and the returned array

For a two-dimensional array, axis=0 means rows and axis=1 means columns. With numpy.delete(array, index, axis=0), index is a zero-based position along the row axis: 0 is the first row, 1 the second, and so on. Omitting axis flattens the array before deletion, which does not remove a complete row from a two-dimensional array.

The operation returns a new array rather than resizing the input. Keep the result by assigning it, or use it directly in a later expression. An index outside the selected axis is out of range; NumPy reports an IndexError. If you are unsure which rows a condition selects, inspect the Boolean mask or print the selected rows before using the result in a larger calculation.