In this blog, we will learn how to use numpy.where() function in Python. This is an important method that can be used in numpy library. This function is basically used to filter and search elements according to the given condition. Let’s learn this method in detail.
Table of Contents
Python numpy.where()
In Python, numpy.where() function is used to select elements based on some conditions, it returns a new array with the elements that fulfill the condition. It is an important function given by numpy library in Python. This function enables us to search, filter, and apply conditions to the elements and returns a new array.
Syntax of Python numpy.where()
Here is the syntax of numpy.where()
numpy.where(condition, [x, y])
Where x and y are the values that should be returned when the condition is true and false respectively. These are optional.
numpy. where() Arguments
This function will take three arguments:
- Condition: a boolean value or take an array
- X: the value returned, if the condition is true.
- Y: the value returned if the condition is false.
numpy.where() Return Value
This function will return a new numpy array.
Shape Your Career as a Data Scientist
Grow with Our Expert- Led Data Science Training
Uses of numpy.where()
We can use this function to search, filter, replace, and conditional checking of the array. It is one of the most important functions given by numpy library in Python.
Example:
Output:
[ 1 3 5 10 10]
Explanation: this code will find in the array if any element is greater than 5, it will replace it with 10, and return the updated error.
Basic Usage Without x and y
We have already learned that x and y are optional, so we can use the where function without the use of x and y. In that case, it will return the indices of the elements that meet the conditions.
Code:
Output:
(array([3, 4]),)
Explanation: In this code, the indices of the element which is greater than 5, are returned.
Using numpy.where() with x and y
By using x and y as arguments, we can return values to the array depending on the condition used. Let’s learn this with an example:
Code:
Output:
[ 0 0 0 10 10]
Explanation: here we have given a condition, and if the condition is true, it will return 10 otherwise 0.
Conditional Selection of Elements from Two Arrays
Free Python Course – Your Gateway to Industry-Level Expertise
Build real-world projects and kickstart your tech career today!
Conditional Selection of Elements from Two Arrays
We can also use the numpy.where() function to select elements from two different arrays based on some condition. Let’s take an example:
Code:
Output:
[2 4 5 7 9]
Explanation: Here, we are checking in arr1. If the element is greater than 5, then the value of arr1 will be inserted; otherwise, the value of arr2.
Practical Examples of numpy.where() in Python
Here are some examples of numpy.where() in Python, let’s learn this in detail:
Example 1: numpy.where() with Operation
Code:
Example1:
[ 1 4 27 16 125]
Explanation: Here if the condition is true, it will give the square of the numbers, otherwise, it will give the cube of the number.
Example 2: numpy.where() with Array Condition
Code:
Output:
[[ 1 2]
[-3 -4]]
Explanation:
- For (0, 0) position: Condition is True, so take value from x = 1.
- For (0, 1) position: Condition is True, so take value from x = 2.
- For (1, 0) position: Condition is False, so take value from y = -3.
- For (1, 1) position: Condition is False, so take value from y = -4.
Example 3: numpy.where() with Multiple Conditions
Code:
Output:
[0 0 1 1 0]
Explanation: here two conditional statements are used, and on the basis of that, the values (1 and 0) are assigned to the array.
If you want to learn more, please refer to this video:
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
So far in this article, we have learned numpy.where() function in Python, its uses, and examples. It is used for conditional statements, and based on the condition, it will return an array. If you want to learn more about nupmy functions, you can enroll in Intellipaat’s Expert-Led Data Science Traning.