You can use boolean indexing.

In [413]: x[x[:, -1] != 0]
Out[413]: 
array([[0, 2, 1],
       [0, 1, 1],
       [1, 0, 2],
       [2, 1, 2]])

  1. x[:, -1] will retrieve the last column

  2. x[:, -1] != 0 returns a boolean mask

  3. Use the mask to index into the original array

Answer from coldspeed95 on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › how can i remove rows from a numpy array based on a condition
r/learnpython on Reddit: how can I remove rows from a numpy array based on a condition
February 27, 2023 -

I have a 2d numpy array M, and some 1d array a, say M = [[1,2], [3,4],[5,6]] and a =[1, 3]. I want to remove the rows of M that have the same element as a in a colom. Thus in this case I would want to remove [1,2]. Thanks

🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Extract or delete elements, rows, and columns that satisfy the conditions | note.nkmk.me
May 31, 2019 - NumPy: Delete rows/columns from an array with np.delete() np.where() returns the index of the element that satisfies the condition.
Discussions

python - Deleting row in numpy array based on condition - Stack Overflow
0 In Python and numpy, how do I remove rows of an array that have a certain condition · 25 Deleting certain elements from numpy array using conditional checks More on stackoverflow.com
🌐 stackoverflow.com
python - How do I remove rows from a numpy array based on multiple conditions? - Stack Overflow
With these, we can combine our ... conditions. ... Sign up to request clarification or add additional context in comments. ... Saullo G. P. Castro · Saullo G. P. Castro Over a year ago · alternatively you could do col0=a[:,0] and a[~((col0>=20) & (col0<=25) & (col0>=30) & (col0<=35))] 2014-08-24T10:56:44.47Z+00:00 ... Find below my solution to the problem of deletion specific rows from a numpy array. The solution is provided as one-liner of the form: # Remove the rows whose ... More on stackoverflow.com
🌐 stackoverflow.com
May 22, 2017
python - deleting rows in numpy array - Stack Overflow
I have an array that might look like this: ANOVAInputMatrixValuesArray = [[ 0.96488889, 0.73641667, 0.67521429, 0.592875, 0.53172222], [ 0.78008333, 0.5938125, 0.481, 0.39883333, 0.]] Notice that... More on stackoverflow.com
🌐 stackoverflow.com
numpy - Python delete a row that meets a condition - Stack Overflow
Communities for your favorite technologies. Explore all Collectives · Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work More on stackoverflow.com
🌐 stackoverflow.com
🌐
GeeksforGeeks
geeksforgeeks.org › python › how-to-remove-rows-from-a-numpy-array-based-on-multiple-conditions
How to remove rows from a Numpy array based on multiple conditions ? - GeeksforGeeks
July 23, 2025 - np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on given index conditions and axis specified, the parameter ndarray is the array on which the manipulation will happen, the index is the particular rows ...
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-remove-rows-from-a-numpy-array-based-on-a-condition-in-python-
How to remove rows from a numpy array based on a condition in python ?
September 17, 2023 - import pandas as pd import numpy ... to eliminate any rows that contain the value -999, regardless of the column in which it appears, a solution is to use any(): ... since the rows with index 2 and 4 contain a value of -999 (keeping in mind that Python uses 0-based index...
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
NumPy: Delete rows/columns from an array with np.delete() | note.nkmk.me
February 5, 2024 - In NumPy, the np.delete() function allows you to delete specific rows, columns, and other elements from an array (ndarray). ... Users must specify the target axis (dimension) and the positions (such as row or column numbers) to be deleted.
Top answer
1 of 4
14

If you want to keep using numpy, the solution isn't hard.

data = data[np.logical_not(np.logical_and(data[:,0] > 20, data[:,0] < 25))]
data = data[np.logical_not(np.logical_and(data[:,0] > 30, data[:,0] < 35))]

Or if you want to combine it all into one statement,

data = data[
    np.logical_not(np.logical_or(
        np.logical_and(data[:,0] > 20, data[:,0] < 25),
        np.logical_and(data[:,0] > 30, data[:,0] < 35)
    ))
]

To explain, conditional statements like data[:,0] < 25 create boolean arrays that track, element-by-element, where the condition in an array is true or false. In this case, it tells you where the first column of data is less than 25.

You can also index numpy arrays with these boolean arrays. A statement like data[data[:,0] > 30] extracts all the rows where data[:,0] > 30 is true, or all the rows where the first element is greater than 30. This kind of conditional indexing is how you extract the rows (or columns, or elements) that you want.

Finally, we need logical tools to combine boolean arrays element-by-element. Regular and, or, and not statements don't work because they try to combine the boolean arrays together as a whole. Fortunately, numpy provides a set of these tools for use in the form of np.logical_and, np.logical_or, and np.logical_not. With these, we can combine our boolean arrays element-wise to find rows that satisfy more complicated conditions.

2 of 4
3

Find below my solution to the problem of deletion specific rows from a numpy array. The solution is provided as one-liner of the form:

#  Remove the rows whose first item is between 20 and 25
A = np.delete(A, np.where( np.bitwise_and( (A[:,0]>=20), (A[:,0]<=25) ) )[0], 0)

and is based on pure numpy functions (np.bitwise_and, np.where, np.delete).

A = np.array( [   [ 18, 6.215, 0.025 ],
    [ 19, 6.203, 0.025 ],
    [ 20, 6.200, 0.025 ],
    [ 21, 6.205, 0.025 ],
    [ 22, 6.201, 0.026 ],
    [ 23, 6.197, 0.026 ],
    [ 24, 6.188, 0.024 ],
    [ 25, 6.187, 0.023 ],
    [ 26, 6.189, 0.021 ],
    [ 27, 6.188, 0.020 ],
    [ 28, 6.192, 0.019 ],
    [ 29, 6.185, 0.020 ],
    [ 30, 6.189, 0.019 ],
    [ 31, 6.191, 0.018 ],
    [ 32, 6.188, 0.019 ],
    [ 33, 6.187, 0.019 ],
    [ 34, 6.194, 0.021 ],
    [ 35, 6.192, 0.024 ],
    [ 36, 6.193, 0.024 ],
    [ 37, 6.187, 0.026 ],
    [ 38, 6.184, 0.026 ],
    [ 39, 6.183, 0.027 ],
    [ 40, 6.189, 0.027 ] ] )

#  Remove the rows whose first item is between 20 and 25
A = np.delete(A, np.where( np.bitwise_and( (A[:,0]>=20), (A[:,0]<=25) ) )[0], 0)

# Remove the rows whose first item is between 30 and 35
A = np.delete(A, np.where( np.bitwise_and( (A[:,0]>=30), (A[:,0]<=35) ) )[0], 0)

>>> A
array([[  1.80000000e+01,   6.21500000e+00,   2.50000000e-02],
       [  1.90000000e+01,   6.20300000e+00,   2.50000000e-02],
       [  2.60000000e+01,   6.18900000e+00,   2.10000000e-02],
       [  2.70000000e+01,   6.18800000e+00,   2.00000000e-02],
       [  2.80000000e+01,   6.19200000e+00,   1.90000000e-02],
       [  2.90000000e+01,   6.18500000e+00,   2.00000000e-02],
       [  3.60000000e+01,   6.19300000e+00,   2.40000000e-02],
       [  3.70000000e+01,   6.18700000e+00,   2.60000000e-02],
       [  3.80000000e+01,   6.18400000e+00,   2.60000000e-02],
       [  3.90000000e+01,   6.18300000e+00,   2.70000000e-02],
       [  4.00000000e+01,   6.18900000e+00,   2.70000000e-02]])
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › how-to-delete-multiple-rows-of-numpy-array
How to delete multiple rows of NumPy array ? - GeeksforGeeks
January 9, 2023 - For doing our task, we will need some inbuilt methods provided by the NumPy module which are as follows: np.delete(ndarray, index, axis): Delete items of rows or columns from the NumPy array based on g ... NumPy arrays offer efficient numerical ...
🌐
YouTube
youtube.com › watch
How to Efficiently Delete Rows in NumPy Arrays Based on Condition from Another Array - YouTube
Learn how to delete specific rows from a NumPy array using conditions from another array in just one line of code!---This video is based on the question http...
Published: May 28, 2025
Views: 1
🌐
Medium
meyerstevenlawrence.medium.com › some-numpy-pandas-tricks-3e5c6192f27e
Some Numpy & Pandas Tricks. How to conditionally delete rows or… | by Steven Meyer | Medium
April 3, 2023 - We want to delete all rows in which the first element is greater than the third. ... import numpy as np #Again make a 2D 10 X 10 array #first create a python list list1 = [5, 10, 9, 5, 2, 0, 0, 3, 5, 9, 10, 0, 5, 10, 8, 6, 9, 1, 7, 0, 6, 4, 5, 6, 9, 7, 3, 0, 3, 10, 2, 6, 7, 5, 7, 8, 0, 7, 7, 6, 10, 6, 3, 8, 2, 9, 8, 0, 1, 9, 10, 0, 1, 5, 5, 0, 8, 9, 10, 2, 10, 10, 9, 0, 3, 6, 7, 5, 9, 10, 6, 4, 5, 2, 4, 5, 2, 8, 7, 9, 6, 4, 1, 10, 6, 0, 4, 8, 3, 1, 7, 0, 7, 10, 2, 3, 10, 2, 1, 3,] #Use list to create numpy array, xArr xArr = np.array(list1).reshape(10,10) #print Array details print() print('Ar
🌐
Moonbooks
en.moonbooks.org › Articles › How-to-remove-rows-from-a-numpy-array-in-python-
How to remove rows from a numpy array in python ?
September 18, 2023 - Create a 2d numpy array · Remove rows using delete() Remove a specific row based on its index · Remove multiples rows · Remove rows based on a column condition · Remove rows using any() Filter out rows based on a specific condition in a particular column ·
🌐
YouTube
youtube.com › hey delphi
Array : How to delete a row based on a condition from a numpy array? - YouTube
Array : How to delete a row based on a condition from a numpy array?To Access My Live Chat Page, On Google, Search for "hows tech developer connect"As promis...
Published: April 16, 2023
Views: 11