Numpy where function with multiple conditions

N

Numpy where function with multiple conditions

Numpy is a popular Python library used for numerical computing. One of the useful functions that Numpy provides is the where function, which allows us to perform conditional operations on arrays. In this article, we will explore how to use the where function with multiple conditions.

Syntax of the where function

The syntax of the where function in Numpy is as follows:

numpy.where(condition, x, y)
  • condition: This is the condition we want to apply on the array.
  • x: This is the value to be used if the condition is True.
  • y: This is the value to be used if the condition is False.

Applying multiple conditions using the where function

We can apply multiple conditions using the logical_and and logical_or functions in Numpy. Let’s see some examples to understand how to use the where function with multiple conditions.

Example 1

import numpy as np

arr = np.array([1, 2, 3, 4, 5, 6])

result = np.where((arr > 2) & (arr < 5), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 2

import numpy as np

arr = np.array([10, 20, 30, 40, 50, 60])

result = np.where((arr % 10 == 0) | (arr < 40), arr, -1)
print(result)

Output:

Numpy where function with multiple conditions

Example 3

import numpy as np

arr = np.array([50, 60, 70, 80, 90, 100])

result = np.where((arr > 70) & (arr % 10 == 0), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 4

import numpy as np

arr = np.array([5, 15, 25, 35, 45, 55])

result = np.where((arr < 10) | (arr > 40), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 5

import numpy as np

arr = np.array([2, 12, 22, 32, 42, 52])

result = np.where((arr % 10 == 2) & (arr > 25), arr, -1)
print(result)

Output:

Numpy where function with multiple conditions

Example 6

import numpy as np

arr = np.array([3, 13, 23, 33, 43, 53])

result = np.where((arr % 10 == 3) | (arr < 20), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 7

import numpy as np

arr = np.array([4, 14, 24, 34, 44, 54])

result = np.where((arr % 10 == 4) & (arr > 40), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 8

import numpy as np

arr = np.array([1, 11, 21, 31, 41, 51])

result = np.where((arr < 15) | (arr > 45), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Example 9

import numpy as np

arr = np.array([6, 16, 26, 36, 46, 56])

result = np.where((arr % 10 == 6) & (arr < 30), arr, -1)
print(result)

Output:

Numpy where function with multiple conditions

Example 10

import numpy as np

arr = np.array([7, 17, 27, 37, 47, 57])

result = np.where((arr % 10 == 7) & (arr > 35), arr, 0)
print(result)

Output:

Numpy where function with multiple conditions

Conclusion of Numpy where function

In conclusion, the where function in Numpy is a powerful tool for performing conditional operations on arrays. By using logical operators like logical_and and logical_or, we can apply multiple conditions to filter and modify arrays efficiently.

Latest Articles

Popular Articles