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


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:

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


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


# 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.



Comment List

  • DataDaft
    December 21, 2020

    very helpful bro..thanks a lot..!!

  • DataDaft
    December 21, 2020

    Well done! 😀

  • DataDaft
    December 21, 2020

    How can you use this to add a new column to a merged data frame. Lets say you wanted to make that array of Highs and lows into its own column in the df. How do you go about that. Thanks

Write a comment