Skip to main content
v2026.11,858 entries · CC-BY 4.0

NumPy Delete: np.delete() Syntax, Rows, Columns and the Boolean-Mask Alternative

How numpy.delete works: remove rows or columns with axis, why omitting axis flattens the result, and when a boolean mask is the better choice.

Written and maintained by CASRAI Editorial Board

Last updated

numpy.delete(arr, obj, axis=None) returns a copy of an array with the entries listed in obj removed along axis. It never changes arr itself, and if you leave axis out it flattens the array first and hands back a 1-D result. Those two behaviours cause most of the confusion around this function, and the rest of this guide is built around them.

What np.delete is used for in research

In research data work, np.delete is the tool for dropping known positions from a numeric array: a header row that was read in as data, a column of identifiers before a matrix is passed to a model, or a handful of observations flagged for exclusion by index. When the rows to drop depend on a condition (for example, missing values), a boolean mask is usually the better tool, and the NumPy documentation itself says so (see below). If you are cleaning a dataset as a whole, the data cleaning steps guide covers the order of operations, and NumPy NaN explains how missing values behave in arrays.

Install or access

NumPy is installed from the official channels listed on the NumPy install page. With pip, run pip install numpy. With conda, run conda install numpy inside an environment you have created and activated. The NumPy install page recommends working inside a virtual environment, created with python -m venv my-env and activated with source my-env/bin/activate on macOS and Linux or my-env\Scripts\activate on Windows. Then import it as usual: import numpy as np.

np.delete step by step

The examples below use this array, taken from the official reference page:

import numpy as np
arr = np.array([[1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12]])

How do you delete a row from a NumPy array?

Pass the row index as obj and set axis=0. Assign the result, because the original is untouched.

arr2 = np.delete(arr, 1, 0)
# [[ 1  2  3  4]
#  [ 9 10 11 12]]

How do you delete a column from a NumPy array?

Use axis=1. The docs example removes every second column with a slice object:

np.delete(arr, np.s_[::2], 1)
# [[ 2  4]
#  [ 6  8]
#  [10 12]]

To remove specific columns, pass a list of indices, for example np.delete(arr, [0, 2], axis=1), which gives the same 3 by 2 result as above on this array. Rows and columns are removed in separate calls: one call removes along one axis.

What happens if you leave out the axis?

The docs state that “If axis is None, obj is applied to the flattened array”, and the returned array is flattened as well. The indices in obj then refer to positions in the flattened array, not to rows or values:

np.delete(arr, [1, 3, 5], None)
# [ 1  3  5  7  8  9 10 11 12]

The call removed flattened positions 1, 3 and 5, which hold the values 2, 4 and 6. If you expected a 2-D array back, you forgot axis.

What does a boolean obj do?

The reference page notes that, since NumPy 1.19.0, boolean indices are treated as a mask of elements to remove rather than being cast to the integers 0 and 1. So True marks an entry to delete. Running np.delete(np.array([10, 20, 30]), [True, False, True]) returns [20] on NumPy 2.5.1. On older code that predates 1.19.0 the same call behaved differently, so check your NumPy version when you reuse an old script.

Output and how to read it

The return value is, in the docs’ words, “A copy of arr with the elements specified by obj removed”, and it notes that delete does not occur in-place. Read the output as follows:

  • With axis set, the result has the same number of dimensions as the input and is shorter by the number of removed entries along that axis.
  • With axis=None, the result is always 1-D.
  • The original array is unchanged, so arr.shape is the same before and after unless you reassign: arr = np.delete(arr, 1, axis=0).

Boolean mask alternative from the NumPy docs

The official notes say it is often preferable to use a boolean mask. Build a mask of ones, switch off the positions to drop, and index with it:

mask = np.ones(len(arr), dtype=bool)
mask[[0, 2]] = False
result = arr[mask, ...]   # same as np.delete(arr, [0, 2], axis=0)

The mask can be reused on a second array of the same length, for example to drop the same participants from a data array and from its matching array of labels. The NumPy indexing guide also states that advanced indexing, which includes boolean indexing, always returns a copy, so this approach has the same copy behaviour as np.delete. Write dtype=bool; the older np.bool alias used in some tutorials was removed from NumPy in 1.24.

For conditions such as removing missing values, the indexing guide shows the pattern directly: for x = np.array([[1., 2.], [np.nan, 3.], [np.nan, np.nan]]), the expression x[~np.isnan(x)] returns array([1., 2., 3.]). Note that this flattens the 2-D array, because a boolean index with the full array’s shape selects individual elements. To drop whole rows that contain any NaN, build the mask per row, for example x[~np.isnan(x).any(axis=1)], which keeps only the first row here. The NumPy ndarray length guide helps when you need to check how many entries remain.

Troubleshooting

Why is my array unchanged after np.delete?

Because the function returns a new array and does not work in place. Assign the result to a name.

Why did I get a 1-D array back?

You did not pass axis, so the input was flattened. Add axis=0 for rows or axis=1 for columns.

Why did it delete the wrong values?

obj holds positions, not values. To remove entries by value, build a mask from a comparison, such as arr[arr != 7], instead of passing the value to np.delete.

Why do I get an IndexError?

An index in obj is beyond the size of the chosen axis. With the 3-row array above, np.delete(arr, 5, axis=0) raises IndexError: index 5 is out of bounds for axis 0 with size 3 on NumPy 2.5.1.

Free alternatives

Approach What it does Source
Boolean mask, arr[mask] Keeps the entries where the mask is True; reusable; returns a copy NumPy numpy.delete notes and indexing guide
x[~np.isnan(x)] Removes NaN entries (flattened result for 2-D input) NumPy indexing guide
pandas.DataFrame.drop Drops labelled rows or columns from a DataFrame; axis 0 or ‘index’ for rows, 1 or ‘columns’ for columns, or the index and columns arguments pandas documentation

FAQ

Does np.delete modify the array in place?

No. It returns a copy with the entries removed, so reassign the result.

How do I delete several rows at once?

Pass a list or array of row indices with axis=0, for example np.delete(arr, [0, 2], axis=0).

Can np.delete remove entries by value?

No, obj takes positions. Use a boolean mask built from a condition to remove by value.

Is np.delete or a boolean mask better?

The NumPy docs say a boolean mask is often preferable, mainly because it can be reused. For a single known index, np.delete is shorter and equally correct. For removing observations in a larger cleaning workflow, see the jackknife guide for a method built around leaving observations out.

Sources

All pages below were checked on 2026-10-29.

Follow CASRAI

Research-administration guidance, standards updates and independent tool reviews.

Ask CASRAI · free to try

Ask about NumPy Delete: np.delete() Syntax, Rows, Columns and the Boolean-Mask Alternative

Ask your first 2 questions free below. Subscribers get 150 a day for $29 a month.

An AI assistant specialized in research administration. It cites the sources behind every answer, labels web answers and says when it can't answer.

Answers draw on CASRAI's guides and dictionary plus the federal and funder documents we index: Federal Register, Grants.gov, Regulations.gov and UKRI.

Works on this site and inside Claude, Cursor and the AI tools you already use.

Everything CASRAI publishes — this page, the dictionary, the guides and the news — stays free to read, with no account and no card.

Referenced across the research world

University of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logoUniversity of Cambridge logoColumbia University logoCrossref logoUniversity of Edinburgh logoHarvard University logoUniversity of Oxford logoPrinceton University logoStanford School of Medicine logoUniversity College London logoORCID logo
  • University of Cambridge logo
  • Columbia University logo
  • Crossref logo
  • University of Edinburgh logo
  • Harvard University logo
  • University of Oxford logo
  • Princeton University logo
  • Stanford School of Medicine logo
  • University College London logo
  • ORCID logo

View CASRAI adoption →