NumPy where with multiple conditions

Numpy where with-multiple condition

Tired of wrangling data in Numpy using nested if, elif and else statements. Making your code bulky and difficult to read code quickly as logic gets tangled. Don’t worry, we have  “Numpy where”, the function can handle data wrangling. Numpy where function is a master of arrays manipulator, effortlessly picking and choosing elements based on conditions. Numpy where with multiple conditions can take your data manipulation to the next level.

We will discuss Numpy where function syntax, Numpy where single condition and Numpy where multiple conditions

Syntax of Numpy.where

numpy.where(condition, x, y)

Numpy where function consists of three major components i.e. condition, x, and y. Condition is a Boolean array which helps to decide which value would be taken from x or y. X is the value or array to be used when condition is true and similarly y is the value or array to be used if condition is False.

Numpy.where with single condition

Now, as we have knowledge of syntax let’s write code for Numpy where with a single condition. Below code demonstrates classification of numbers greater than and lesser than 5.

# Creating a array of random number using numpy

import numpy as np
arr = np.random.randint(0, 10, size=10)
arr
Output : array([1, 2, 1, 0, 1, 5, 7, 7, 2, 6])
# applying a condition of classifying number greater than or less than 5

condition=arr>5

results=np.where(arr>5,'greater than 5','lesser than 5')

results
Output : array(['lesser than 5', 'lesser than 5', 'lesser than 5', 
'lesser than 5', 'lesser than 5','lesser than 5', 'greater than 5', 
'greater than 5', 'lesser than 5', 'greater than 5'])

#printing the number and output from np.where function together

for index in range(len(arr)):
    print(f"{arr[index]} is {results[index]}")
Output:
1 is lesser than 5
2 is lesser than 5
1 is lesser than 5
0 is lesser than 5
1 is lesser than 5
5 is lesser than 5
7 is greater than 5
7 is greater than 5
2 is lesser than 5
6 is greater than 5

Numpy.where with multiple condition

We are always facing complex scenarios where we need to combine multiple conditions to get the correct output data.

We will use logical operators to construct complex conditions:

  • & (bitwise AND): Combines conditions to find elements where both are True.
  • | (bitwise OR): Combines conditions to find elements where either is True.
  • ~ (bitwise NOT): Invert the boolean condition.

Now, Let’s code for the “Numpy.where” function with multiple conditions with the logical operators.

Numpy.where with bitwise AND condition

Demonstrating the code for getting an even number greater than 3. I have combined two condition with (bitwise AND ) operation that are 

(arr > 3): arr element greater than 3 

(arr % 2 == 0) : gives the remainder when arr is divided by 2

If both the conditions are true output comes as ‘Even and greater than 3’ and if one of the conditions is false it returns ‘Other’.

arr = np.array([2, 5, 1, 8, 3])
result = np.where((arr > 3) & (arr % 2 == 0), 'Even and greater than 3', 'Other')
print(result)  

# Output: ['Other','Even and greater than 3','Other',
'Even and greater than 3','Other']

Numpy.where with bitwise OR condition:

Below code checks if the array elements are divisible by 3 or 7. If divisible output is shown as ‘Number is divisible by 3 or 7’ else ‘Number is not divisible by 3 or 7’

arr = np.array([10, -5, 2, 0, 7])
result = np.where((arr % 3==0) | (arr % 7==0), 'Number is divisible by 3 or 7', 
'Number is not divisible by 3 or 7')
print(result)
 
#Output: ['Number is not divisible by 3 or 7','Number is not divisible by 3 or 7',
'Number is not divisible by 3 or 7','Number is divisible by 3 or 7',
'Number is divisible by 3 or 7']

Numpy.where with bitwise NOT condition:

Below code finds elements in the array that are not divisible by 3 and also greater than 2

arr = np.array([1, 2, 3, 4, 5])
result = np.where(~(arr % 3 == 0) & (arr > 2), 'Meets criteria', 
'Does not meet criteria')

print(result)  

#Output: ['Does not meet criteria','Does not meet criteria',
'Does not meet criteria','Meets criteria','Meets criteria']

Key points to remember are when using Numpy.where() function

  • Use parentheses to clarify complex conditions.
  • Numpy where() returns elements from x or y based on condition. Value x is returned when the condition  becomes  true and similarly y is returned when condition is false.
  • For multiple conditions, combine them using logical operators within the condition.
  • Numpy where() can be used with multi-dimensional arrays, applying conditions element-wise.
  • Numpy where() function is faster than nested if else as it uses vectorization.

Numpy where() function example

We will write code to find whether a sentence contains the keyword ‘gold’ and the length of the sentence is greater than 7 characters. We create two conditions separate and call it in numpy.where() function.

import numpy as np

# Sample sentences
sentences = np.array(["The gold is valuable.",
 "This is a test sentence.",
 "I found a golden ring."])

# Check for the keyword 'gold' in each sentence
contains_gold = np.char.find(sentences, 'gold') >= 0

# Get the length of each sentence and check if it greater than 7
sentence_lengths = np.char.str_len(sentences)
sentence_flag=sentence_lengths>7

# Use numpy.where() to find sentences that meet both conditions
indices = np.where(contains_gold & sentence_flag)

print("Indices of sentences containing 'gold' and
 having length less than 7:", indices)
Output:
Indices of sentences containing 'gold' and having length
 less than 7: (array([0, 2], dtype=int64),)

Create a Python mini-project for securing your password by  following the below blog.

Python random password generator

Conclusion: 

Numpy where() function handles complex multiple conditions in a single line which makes code readable and concise. Making you a better programmer. You can find the above code on GitHub.

1 thought on “NumPy where with multiple conditions”

  1. Pingback: How to add a row to a dataframe in python

Comments are closed.