NumPy - Filtering rows by multiple conditions
Last Updated :
10 Oct, 2022
In this article, we will discuss how to filter rows of NumPy array by multiple conditions. Before jumping into filtering rows by multiple conditions, let us first see how can we apply filter based on one condition. There are basically two approaches to do so:
Method 1: Using mask array
The mask function filters out the numbers from array arr which are at the indices of false in mask array. The developer can set the mask array as per their requirement--it becomes very helpful when it is tough to form a logic of filtering.
Approach
- Import module
- Make initial array
- Define mask
- Make a new array based on the mask
- Print new array
Program:
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 20)])
print("Original array")
print(arr)
# defining mask
mask = [True, False, True, False, True, True, False, False, False]
# making new array on conditions
new_arr = arr[mask]
print("New array")
print(new_arr)
Output
Original array
[11 12 13 14 15 16 17 18 19]
New array
[11 13 15 16]
Method 2: Using iterative method
Rather than using masks, the developer iterates the array arr and apply condition on each of the array element.
Approach
- Import module
- Create array
- Create an empty array
- Iterate through array
- Select items based on some condition
- Add selected items to the empty array
- Display array
Program:
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 20)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying condition: appending even numbers
if x % 2 == 0:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
Output
Original array
[11 12 13 14 15 16 17 18 19]
New array
[12 14 16 18]
Now let's try to apply multiple conditions on the NumPy array
Method 1: Using mask
Approach
- Import module
- Create initial array
- Define mask based on multiple conditions
- Add values to the new array according to the mask
- Display array
Example
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# defining mask based on two conditions:
# array element must be greater than 15
# and must be a divisible by 2
mask = (arr > 15) & (arr % 2 == 0)
# making new array on conditions
new_arr = arr[mask]
print("New array")
print(new_arr)
Output
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
Method 2: Iterative method
Approach
- Import module
- Create initial array
- Create an empty array
- Iterate through the array
- Select items based on multiple conditions
- Add selected items to the empty list
- Display array
Example
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# making a blank list
new_arr = []
for x in arr:
# applying two conditions: number is divisible by 2 and is greater than 15
if x % 2 == 0 and x > 15:
new_arr.append(x)
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
Output
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 20 22 24 26 28 30 32 34 36 38]
Method 3: Using lambda
Approach
- Import module
- Create initial array
- Apply multiple conditions using lambda function
- Select items accordingly
- Add items to a new array
- Display array
Example
Python3
# importing numpy lib
import numpy as np
# making a numpy array
arr = np.array([x for x in range(11, 40)])
print("Original array")
print(arr)
# using lambda to apply condition
new_arr = list(filter(lambda x: x > 15 and x % 2 == 0 and x % 10 != 0, arr))
# Converting new list into numpy array
new_arr = np.array(new_arr)
print("New array")
print(new_arr)
Output
Original array
[11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
35 36 37 38 39]
New array
[16 18 22 24 26 28 32 34 36 38]
Similar Reads
Filter data by multiple conditions in R using Dplyr In this article, we will learn how can we filter dataframe by multiple conditions in R programming language using dplyr package. The filter() function is used to produce a subset of the data frame, retaining all rows that satisfy the specified conditions. The filter() method in R programming languag
3 min read
Delete rows in PySpark dataframe based on multiple conditions In this article, we are going to see how to delete rows in PySpark dataframe based on multiple conditions. Method 1: Using Logical expression Here we are going to use the logical expression to filter the row. Filter() function is used to filter the rows from RDD/DataFrame based on the given conditio
2 min read
Pyspark - Filter dataframe based on multiple conditions In this article, we are going to see how to Filter dataframe based on multiple conditions. Let's Create a Dataframe for demonstration: Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSession # creating sparksession and giving an
3 min read
Filter Pandas Dataframe with multiple conditions In this article, let's discuss how to filter pandas dataframe with multiple conditions. There are possibilities of filtering data from Pandas dataframe with multiple conditions during the entire software development. Filter Pandas Dataframe with multiple conditionsThe reason is dataframe may be havi
6 min read
Filter Rows Based on Conditions in a DataFrame in R In this article, we will explore various methods to filter rows based on Conditions in a data frame by using the R Programming Language. How to filter rows based on Conditions in a data frame R language offers various methods to filter rows based on Conditions in a data frame. By using these methods
3 min read
Filtering Data Using Conditions Joined by AND Operator In the world of database management, precision is important. Whether we are dealing with customer records, financial transactions, or inventory data, the ability to retrieve specific information quickly is essential. SQL or Structured Query Language provides powerful tools for filtering data from da
5 min read
How to remove rows from a Numpy array based on multiple conditions ? 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 g
3 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
Python PySpark - DataFrame filter on multiple columns In this article, we are going to filter the dataframe on multiple columns by using filter() and where() function in Pyspark in Python. Creating Dataframe for demonestration: Python3 # importing module import pyspark # importing sparksession from pyspark.sql module from pyspark.sql import SparkSessio
2 min read
Select Rows With Multiple Filters in Pandas In this article, we are going to select rows using multiple filters in pandas. We will select multiple rows in pandas using multiple conditions, logical operators and using loc() function. Selecting rows with logical operators i.e. AND and OR can be achieved easily with a combination of >, <,
3 min read