How to remove rows from a Numpy array based on multiple conditions ?
Last Updated :
03 Jul, 2021
In this article, we will learn how to remove rows from a NumPy array based on multiple conditions. 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 given index conditions and axis specified, the parameter ndarray is the array on which the manipulation will happen, the index is the particular rows based on conditions to be deleted, axis=0 for removing rows in our case.
- np.where(conditions): Operate on array items depending on conditions on rows or columns depending on the axis given.
Note: For 2-dimensional NumPy arrays, rows are removed if axis=0, and columns are removed if axis=1. But here we intend is to remove rows, so we will keep axis=0.
Let us take the NumPy array sample. Here we have taken a NumPy array having elements from 0 to 40 and reshaped the array into 8 rows and 5 columns.
Python3
import numpy as np
nparray = np.arange(40).reshape((8, 5))
print("Given numpy array:\n", nparray)
Output:
Example 1: Remove rows having elements between 5 and 20 from the NumPy array
Here np.where((nparray >= 5) & (nparray <= 20))[0], axis=0) means it will delete the rows in which there is at least one or more elements that is greater than or equal to 5 and less than or equal to 20. So, 2nd, 3rd,4th, and 5th rows have elements according to the conditions given, so it gets deleted or removed.
Python3
nparray = np.delete(nparray, np.where(
(nparray >= 5) & (nparray <= 20))[0], axis=0)
print("After deletion of rows containing
numbers between 5 and 20: \n", nparray)
Output:
Example 2: Remove rows whose first element is greater than 25 and less than 35 from the NumPy array
Here (np.where(nparray[:, 0] >= 25) & (nparray[:, 0] <= 35))[0], axis=0)means it will delete the rows in which there is at least one or more elements whose first element is greater than or equal to 25 and less than to equal to 35. The nparray[:, 0] to point to the first element of each row. So, 6th, 7th, 8th rows have elements according to the conditions given, so it gets deleted or removed.
Python3
nparray = np.delete(nparray, np.where(
(nparray[:, 0] >= 25) & (nparray[:, 0] <= 35))[0], axis=0)
print("After deletion of rows whose first element \
is between 25 and 35:\n", nparray)
Output:
Example 3: Remove rows whose third item is divisible by 2, 5th and 4th items are divisible by 3
Here np.where((nparray[:, 2] % 2 == 0) | (nparray[:, 4] % 3 == 0)| (nparray[:, 3] % 3 == 0))[0], axis=0) means it will delete the rows in which there is at least one or more elements whose 3rd column item(s) are divisible by 3 or rows in which there is at least one or more elements whose 5th and 4th column item(s) are divisible by 3 axis=0 to remove the rows. The nparray[:, 2], nparray[:, 4], nparray[:, 3] to point to the third, 5th, and 4th item of each row respectively. So, 6th, 7rd, 8th rows have elements according to the conditions given, so it gets deleted or removed.
Python3
nparray = np.delete(nparray, np.where((nparray[:, 2] % 2 == 0) | (
nparray[:, 4] % 3 == 0) | (nparray[:, 3] % 3 == 0))[0], axis=0)
print("After removing required rows :\n", nparray)
Output:
Similar Reads
How to filter two-dimensional NumPy array based on condition ?
In this article, we are going to see how to apply the filter by the given condition in NumPy two-dimensional array. We have to obtain the output of required elements i.e., whatever we want to filter the elements from the existing array or new array. Here we are going to create a two-dimensional arra
4 min read
How to remove specific elements from a NumPy array ?
In this article, we will discuss how to remove specific elements from the NumPy Array. Remove specific elements from a NumPy 1D arrayDeleting element from NumPy array using np.delete() The delete(array_name ) method will be used to do the same. Where array_name is the name of the array to be delete
3 min read
How to remove array rows that contain only 0 using NumPy?
Numpy library provides a function called numpy.all() that returns True when all elements of n-d array passed to the first parameter are True else it returns False. Thus, to determine the entire row containing 0's can be removed by specifying axis=1. It will traverse each row and will check for the c
2 min read
How to Remove Duplicate Elements from NumPy Array
In this article, we will see how to remove duplicate elements from NumPy Array. Here we will learn how to Remove Duplicate Elements from a 1-D NumPy Array and 2-D NumPy Array. Input1: [1 2 3 4 5 1 2 3 1 2 9]Output1: [1 2 3 4 5 9]Explanation: In this example, we have removed duplicate elements from o
7 min read
How to use NumPy where() with multiple conditions in Python ?
In Python, NumPy has a number of library functions to create the array and where is one of them to create an array from the satisfied conditions of another array. The numpy.where() function returns the indices of elements in an input array where the given condition is satisfied. Syntax: numpy.where(
3 min read
How to delete last N rows from Numpy array?
In this article, we will discuss how to delete the last N rows from the NumPy array. Method 1: Using Slice Operator Slicing is an indexing operation that is used to iterate over an array. Â Syntax: array_name[start:stop] where start is the start is the index and stop is the last index. We can also do
4 min read
How to Remove rows in Numpy array that contains non-numeric values?
Many times NumPy arrays may contain NaN values that need to be removed to ensure the array is free from unnecessary or invalid data. This can be achieved using the np.isnan() function along with the Bitwise NOT operator. Note that this approach specifically targets NaN values and may not handle othe
2 min read
How to access different rows of a multidimensional NumPy array?
Let us see how to access different rows of a multidimensional array in NumPy. Sometimes we need to access different rows of multidimensional NumPy array-like first row, the last two rows, and even the middle two rows, etc. In NumPy , it is very easy to access any rows of a multidimensional array. Al
3 min read
How to Remove columns in Numpy array that contains non-numeric values?
Many times we have non-numeric values in NumPy array. These values need to be removed, so that array will be free from all these unnecessary values and look more decent. It is possible to remove all columns containing Nan values using the Bitwise NOT operator and np.isnan() function. Example 1: Pyth
2 min read
How to get values of an NumPy array at certain index positions?
Sometimes we need to remove values from the source Numpy array and add them at specific indices in the target array. In NumPy, we have this flexibility, we can remove values from one array and add them to another array. We can perform this operation using numpy.put() function and it can be applied t
4 min read