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
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 Select Rows Based on a Logical Condition in Pandas (Python)
↓ Code Available Below! ↓
This video shows how to select the rows of a data frame based on a logical condition, such as whether the value of a column conform to a given logical expression. Filtering or subsetting the rows of a data frame by the values of one or more columns is a common an useful operation that youll perform regularly when cleaning, manipulating and exploring data.
If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1
Code used in this Python Code Clip:
data = pd.DataFrame({character: [Goku,Vegeta, Nappa,Gohan,
Piccolo,Tien,Yamcha, Krillin],
power_level: [12000, 16000, 4000, 1500, 3000,
2000, 1600, 2000],
uniform color: [orange, blue, black, orange,
purple, green, orange, orange],
species: [saiyan,saiyan,saiyan,half saiyan,
namak,human,human,human]})
data
# Create a logical index with one value for each row
logical_index = data[power_level] > 2000
logical_index
# Use the logical index to index into the data frame
data[logical_index]
# Select rows based on multiple logical conditions
logical_condition_1 = data[power_level] > 2000
logical_condition_2 = data[species] != saiyan
data[logical_condition_1 & logical_condition_2]
* 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: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only How to Select Rows Based on a Logical Condition in Pandas (Python)](https://i.ytimg.com/vi/g8v5c9zmpJI/mqdefault.jpg)





![Python Regex: How To Remove Punctuation
↓ Code Available Below! ↓
This video shows how to match and strip punctuation from a text string using the regular expressions package in Python. Text data often contains punctuation that you might want to strip out of text as a preprocessing step before splitting the text into its constituent words for data analysis. The Python regular expressions package makes it easy to match and replace punctuation with empty strings, effectively stripping out all punctuation.
If you find this video useful, like, share and subscribe to support the channel!
► Subscribe: https://www.youtube.com/c/DataDaft?sub_confirmation=1
Code used in this Python Code Clip:
import re
lines =
Nappa @ Vegeta: What does the scouter say about his power level?
Vegeta @ Nappga: Its over (9000!)
Nappa @ Vegeta: [What 9000?] That cant be right... Can it?
# Match and strip punctuation with re.sub()
re.sub(pattern = [^ws],
repl = ,
string = lines)
# Match and strip punctuation and whitespace with re.sub()
re.sub(pattern = W,
repl = ,
string = lines)
* Note you can access some common punctuation characters using:
import string
string.punctuation
** 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: https://www.kite.com/get-kite/?utm_medium=referral&utm_source=youtube&utm_campaign=datadaft&utm_content=description-only Python Regex: How To Remove Punctuation](https://i.ytimg.com/vi/hxU3HLX1W1k/mqdefault.jpg)