How to use NumPy where() with multiple conditions in Python ?
Last Updated :
05 Apr, 2021
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(condition[, x, y])
Parameters:
- condition : When True, yield x, otherwise yield y.
- x, y : Values from which to choose. x, y and condition need to be broadcastable to some shape.
Returns: [ndarray or tuple of ndarrays] If both x and y are specified, the output array contains elements of x where condition is True, and elements from y elsewhere.
If the only condition is given, return the tuple condition.nonzero(), the indices where the condition is True. In the above syntax, we can see the where() function can take two arguments in which one is mandatory and another one is optional. If the value of the condition is true an array will be created based on the indices.
Example 1:
Numpy where() with multiple conditions using logical OR.
Python3
# Import NumPy library
import numpy as np
# Create an array using the list
np_arr1 = np.array([23, 11, 45, 43, 60, 18,
33, 71, 52, 38])
print("The values of the input array :\n", np_arr1)
# Create another array based on the
# multiple conditions and one array
new_arr1 = np.where((np_arr1))
# Print the new array
print("The filtered values of the array :\n", new_arr1)
# Create an array using range values
np_arr2 = np.arange(40, 50)
# Create another array based on the
# multiple conditions and two arrays
new_arr2 = np.where((np_arr1), np_arr1, np_arr2)
# Print the new array
print("The filtered values of the array :\n", new_arr2)
Output:
Example 2:
Numpy where() with multiple conditions using logical AND.
Python3
# Import NumPy library
import numpy as np
# Create two arrays of random values
np_arr1 = np.random.rand(10)*100
np_arr2 = np.random.rand(10)*100
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array based on the conditions
new_arr = np.where((np_arr1), np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
Output:
Example 3:
Numpy where() with multiple conditions in multiple dimensional arrays.
Python3
# Import NumPy library
import numpy as np
# Create two multidimensional arrays of
# integer values
np_arr1 = np.array([[6, 13, 22, 7, 12],
[7, 11, 16, 32, 9]])
np_arr2 = np.array([[44, 20, 8, 35, 10],
[98, 23, 42, 6, 13]])
# Print the array values
print("\nThe values of the first array :\n", np_arr1)
print("\nThe values of the second array :\n", np_arr2)
# Create a new array from two arrays based on
# the conditions
new_arr = np.where(((np_arr1 % 2 == 0) & (np_arr2 % 2 == 1)),
np_arr1, np_arr2)
# Print the new array
print("\nThe filtered values of both arrays :\n", new_arr)
Output:
Conclusion:
The where() function in NumPy is used for creating a new array from the existing array with multiple numbers of conditions.
Similar Reads
Check multiple conditions in if statement - Python If-else conditional statement is used in Python when a situation leads to two conditions and one of them should hold true. Syntax:if (condition): code1else: code2[on_true] if [expression] else [on_false]Note: For more information, refer to Decision Making in Python (if , if..else, Nested if, if-elif
4 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 check multiple variables against a value in Python? Given some variables, the task is to write a Python program to check multiple variables against a value. There are three possible known ways to achieve this in Python: Method #1: Using or operator This is pretty simple and straightforward. The following code snippets illustrate this method. Example
2 min read
numpy.ma.where() function - Python numpy.ma.where() function return a masked array with elements from x or y, depending on condition. Syntax : numpy.ma.where(condition, x, y) Parameter : condition : [array_like, bool] Where True, yield x, otherwise yield y. x, y : [array_like, optional] Values from which to choose. x, y and condition
1 min read
Python | Find elements within range in numpy Given numpy array, the task is to find elements within some specific range. Let's discuss some ways to do the task. Method #1: Using np.where() Python3 1== # python code to demonstrate # finding elements in range # in numpy array import numpy as np ini_array = np.array([1, 2, 3, 45, 4, 7, 810, 9, 6]
2 min read