How to Use where() in Numpy and Pandas (Python) @DataDaft
How to Use where() in Numpy and Pandas (Python)  @DataDaft
Uploaded September 2020 | Updated November 2026, 2 hours ago
This video shows how to use the where() function in numpy and pandas to extract indices based on logical conditions and populate new columns of data based on elementwise logic. The np.where() function can perform a similar operation to the ifelse() function in R.

If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: youtube.com/c/DataDaft?sub_confirmation=1


Code used in this Python Code Clip:

import numpy as np
import pandas as pd

import statsmodels.api as sm #(To access mtcars dataset)
mtcars = sm.datasets.get_rdataset("mtcars", "datasets", cache=True).data

mtcars.head()

# Extract indices that meet a condition
inds = np.where(mtcars.mpg > 22)

inds

# Perform operations across an array or column based on a condition

np.where(mtcars.mpg > 22, # Condition
"High MPG", # Value to set if condition is True
"Low MPG") # Value to set if condition is False

# Perform elementwise operations on an array or column

np.where(mtcars.mpg > 22, # Condition
mtcars.mpg, # Value to set if condition is True
mtcars.cyl) # Value to set if condition is False



* Note: YouTube does not allow greater than or less than symbols in the text description, so the code above will not be exactly the same as the code shown in the video! I will use Unicode large < and > symbols in place of the standard sized ones. .


⭐ Kite is a free AI-powered coding assistant that integrates with popular editors and IDEs to give you smart code completions and docs while you’re typing. It is a cool application of machine learning that can also help you code faster! Check it out here: kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only
How to Use where() in Numpy and Pandas (Python)Top 150 One Piece Characters by Panel Appearances (Through Chapter 1000)Python for Data Analysis: Basic Data TypesPython for Data Analysis: Dealing With DatesHow To Add Text To a Plot in RHow to Select Rows Based on a Logical Condition in Pandas (Python)Python Programming Practice:  LeetCode #104   Maximum Depth of Binary TreeHow to Make a For Loop in PythonTop 15 MMA Fighters Over Time Ranked by Elo (1980-2020)Python Programming Practice: Leetcode #15   3SumPython for Data Analysis: Tuples and StringsPython Regex: How To Remove Punctuation
DataDaft |

How to Use where() in Numpy and Pandas (Python)

SHARE TO X SHARE TO REDDIT SHARE TO FACEBOOK WALLPAPER