where() Function in Python: Complete Guide with ExamplesNumPy is one of the most important Python libraries used in Data Science, Machine Learning, Artificial Intelligence, Data Analytics, and Scientific Computing.
It provides powerful tools for working with arrays and performing high-performance numerical operations.
One of the most commonly used NumPy functions is:
numpy.where()
The where() function allows you to apply conditions to arrays and return values based on those conditions.
In this guide, you'll learn:
What the NumPy where() function is
Syntax of where()
Practical examples
Conditional filtering
Real-world applications
Interview questions
Best practices
where() Function?The where() function is used to return elements from an array based on specified conditions.
It works similarly to:
if-else conditions
but performs operations efficiently on entire arrays.
The function is commonly used for:
Data filtering
Conditional replacement
Data transformation
Machine Learning preprocessing
where()?Without NumPy:
You may need loops and multiple conditions.
With NumPy:
Faster execution
Cleaner code
Vectorized operations
Better performance on large datasets
This makes it extremely useful in Data Science projects.
where()Basic syntax:
numpy.where(
condition,
value_if_true,
value_if_false
)
Parameters:
| Parameter | Description |
|---|---|
| condition | Condition to evaluate |
| value_if_true | Returned if condition is True |
| value_if_false | Returned if condition is False |
Before using NumPy:
import numpy as np
where()import numpy as np
arr = np.array(
[10, 20, 30, 40, 50]
)
result = np.where(
arr > 30,
"High",
"Low"
)
print(result)
Output:
['Low' 'Low' 'Low' 'High' 'High']
Explanation:
Values greater than 30 become "High"
Others become "Low"
where() with NumbersExample:
import numpy as np
arr = np.array(
[1, 2, 3, 4, 5]
)
result = np.where(
arr % 2 == 0,
100,
0
)
print(result)
Output:
[0 100 0 100 0]
Even numbers become:
100
Odd numbers become:
0
where()where() can also return index positions.
Example:
import numpy as np
arr = np.array(
[5, 10, 15, 20, 25]
)
result = np.where(
arr > 15
)
print(result)
Output:
(array([3, 4]),)
Meaning:
Values greater than 15 are located at:
Index 3
Index 4
Example:
import numpy as np
arr = np.array(
[10, 20, 30, 40, 50]
)
result = np.where(
(arr > 20) &
(arr < 50),
"Valid",
"Invalid"
)
print(result)
Output:
['Invalid'
'Invalid'
'Valid'
'Valid'
'Invalid']
where()Example:
import numpy as np
salary = np.array(
[25000, 40000, 15000, 50000]
)
updated_salary =
np.where(
salary < 20000,
20000,
salary
)
print(updated_salary)
Output:
[25000 40000 20000 50000]
This replaces salaries below:
20000
with:
20000
where() with StringsExample:
import numpy as np
names = np.array(
["Rahul", "Priya", "Amit"]
)
result = np.where(
names == "Rahul",
"Found",
"Not Found"
)
print(result)
Output:
['Found'
'Not Found'
'Not Found']
where() in Data ScienceData Scientists frequently use where() for:
Data cleaning
Missing value handling
Feature engineering
Data transformation
Label creation
Example:
import numpy as np
marks = np.array(
[45, 60, 30, 80]
)
result = np.where(
marks >= 40,
"Pass",
"Fail"
)
print(result)
Output:
['Pass'
'Pass'
'Fail'
'Pass']
where()Example:
High Value Customer
Low Value Customer
based on purchase amount.
Label transactions:
Suspicious
Normal
based on risk score.
Classify patients based on:
Age
Risk factors
Medical conditions
Used during:
Feature engineering
Target variable creation
Data preprocessing
| Python if-else | NumPy where() |
|---|---|
| Works on single values | Works on arrays |
| Uses loops for arrays | Vectorized operations |
| Slower | Faster |
| Less efficient for large datasets | Highly optimized |
Incorrect:
where(arr > 10)
Correct:
import numpy as np
np.where(arr > 10)
Wrong:
arr > 10 and arr < 50
Correct:
(arr > 10) &
(arr < 50)
where() Interview Questionswhere() function?The where() function performs conditional operations on arrays and returns values based on specified conditions.
where()?np.where(
condition,
value_if_true,
value_if_false
)
where() return indexes?Yes.
If only a condition is provided:
np.where(arr > 10)
it returns matching indexes.
where() faster than loops?Because NumPy uses vectorized operations optimized in C.
where() used in Data Science?Used for:
Data preprocessing
Feature engineering
Conditional filtering
Data transformation
where()Use vectorized operations instead of loops.
Keep conditions readable.
Use parentheses for multiple conditions.
Combine with Pandas and NumPy workflows.
Test logic on sample datasets first.
NumPy is one of the foundational libraries for:
Data Science
Machine Learning
Artificial Intelligence
Scientific Computing
Data Analytics
Most advanced libraries such as:
Pandas
Scikit-Learn
TensorFlow
PyTorch
are built on top of NumPy.
Learning NumPy helps build strong foundations for AI and Data Science careers.
The NumPy where() function is one of the most useful tools for performing conditional operations on arrays efficiently. It simplifies data filtering, transformation, labeling, and preprocessing tasks while improving performance compared to traditional loops.
Whether you're preparing for Data Science interviews, working on Machine Learning projects, or learning Python for Analytics, mastering the where() function will help you write cleaner, faster, and more efficient code.