Try this: Using the setup from @Maxu

col         = 'consumption_energy'
conditions  = [ df2[col] >= 400, (df2[col] < 400) & (df2[col]> 200), df2[col] <= 200 ]
choices     = [ "high", 'medium', 'low' ]
    
df2["energy_class"] = np.select(conditions, choices, default=np.nan)


  consumption_energy energy_class
0                 459         high
1                 416         high
2                 186          low
3                 250       medium
4                 411         high
5                 210       medium
6                 343       medium
7                 328       medium
8                 208       medium
9                 223       medium
Answer from Merlin on Stack Overflow
🌐
Statology
statology.org › home › pandas: how to use equivalent of np.where()
Pandas: How to Use Equivalent of np.where()
June 24, 2022 - import numpy as np #create NumPy array of values x = np.array([1, 3, 3, 6, 7, 9]) #update valuesin array based on condition x = np.where((x < 5) | (x > 8), x/2, x) #view updated array x array([0.5, 1.5, 1.5, 6. , 7. , 4.5]) If a given value in the array was less than 5 or greater than 8, we divided the value by 2. Else, we left the value unchanged. We can perform a similar operation in a pandas DataFrame by using the pandas where() function, but the syntax is slightly different.
Discussions

python - Numpy "where" with multiple conditions - Stack Overflow
I try to add a new column "energy_class" to a dataframe "df_energy" which contains the string "high" if the "consumption_energy" value > 400, "medium... More on stackoverflow.com
🌐 stackoverflow.com
Python : Using np.where() with conditions
Hello! I am attempting to practice some hypothesis testing on a data set where one of the columns I’m trying to clean is gender. It looks like it was an open field where individuals were allowed to type in their gender. I am not too familiar with replacing 45+ different unique values, so ... More on discuss.codecademy.com
🌐 discuss.codecademy.com
0
0
January 13, 2022
python - Nested np.where - Stack Overflow
I have the following dataframe: S A 1 1 1 0 2 1 2 0 I wanted to create a new 'Result' column that is calculated based on the values of both column A and column S. I wrote the following nested np.... More on stackoverflow.com
🌐 stackoverflow.com
how to update a pandas dataframe column value, when a specific string appears in another column?
It's not something you'd really use .apply for. You would use boolean indexing, e.g. df['A'].str.contains('foo') would give you a Series of True/False values. You can then use .loc to set column(s) to a particular value for the True rows: df.loc[df['A'].str.contains('foo'), 'B'] = 'bar' More on reddit.com
🌐 r/learnpython
7
3
July 24, 2024
🌐
Python Guides
pythonguides.com › python-numpy-where
Optimizing Data Analysis in Pandas Using np.where() in 2025
May 16, 2025 - When used with Pandas, the function returns a new array with elements chosen from x or y depending on the condition. Let’s start with a simple example using a dataset of sales from different US states: import pandas as pd import numpy as np # Create a sample DataFrame data = { 'State': ...
🌐
StrataScratch
stratascratch.com › blog › exploring-numpy-where-in-python
Exploring NumPy where() in Python for Conditional Operations - StrataScratch
September 17, 2025 - When cleaning data, engineering features, making conditional replacements, or masking processes on large arrays · Let's start with something simple. When you offer np.where() a condition, it gives you the indices where that condition is true.
🌐
IncludeHelp
includehelp.com › python › numpy-where-function-multiple-conditions.aspx
Python - NumPy 'where' function multiple conditions
To tackle the problem of comparing two conditions only, we check the value with np.where() condition to check all the three conditions and assign the values to them. ... # Importing pandas package import pandas as pd # Import numpy package import numpy as np # Creating a Dictionary d = ...
🌐
Medium
medium.com › @stacymacbrains › pandas-where-vs-numpy-where-df68efcb580f
Pandas where vs. NumPy where. Pandas where and NumPy where are… | by Ogochukwu Stanley Ikegbo | Medium
November 25, 2024 - Pandas where(): Ideal for conditional replacement within DataFrames, especially when working with mixed data types. NumPy where(): Best suited for numerical array operations, conditional filtering, and creating new arrays based on conditions.
🌐
Codegive
codegive.com › blog › pandas_numpy_where.php
Mastering pandas numpy where: Unlock Advanced Conditional Logic in Pandas DataFrames (2024) – Boost Your Data Analysis Efficiency Today!
March 27, 2026 - Q: Does np.where modify my original Pandas DataFrame in place? A: No, np.where returns a new NumPy array based on the conditions and chosen values. To apply the changes to your DataFrame, you must assign the result of np.where back to a new or existing column (e.g., df['new_col'] = np.where(...)).
Find elsewhere
🌐
Medium
medium.com › @shouke.wei › mastering-np-where-and-pd-where-in-python-conditional-selection-made-easy-8a8666094040
Mastering np.where() and pd.where() in Python
November 2, 2025 - Pandas · Data Analysis · Tips And Tricks · Dr. Shouke Wei · 3 min read · ·Nov 2, 2025 · -- Listen · Share · Press enter or click to view image in full size · When working with large datasets or numerical arrays in Python, it’s common to need conditional logic — selecting, replacing, or filtering values based on specific conditions. Two powerful functions, np.where() (from NumPy) and pd.where() (from Pandas), make this task elegant, efficient, and expressive.
🌐
Note.nkmk.me
note.nkmk.me › home › python › numpy
numpy.where(): Manipulate elements depending on conditions | note.nkmk.me
November 5, 2019 - You can apply multiple conditions with np.where() by enclosing each condition in () and using & or |. print(np.where((a > 2) & (a < 6), -1, 100)) # [[100 100 100] # [ -1 -1 -1] # [100 100 100]] print(np.where((a > 2) & (a < 6) | (a == 7), -1, ...
🌐
Position Is Everything
positioniseverything.net › home › numpy where multiple conditions: a complete beginner’s guide
Numpy Where Multiple Conditions: A Complete Beginner’s Guide - Position Is Everything
December 29, 2025 - Suppose you have an array of five numbers and you want to update numbers that are completely divisible by 2 with a number that is 3 times the original value. Here is an example of how you can go about it with np.where multiple conditions replace: If you run this code, the outcome will be [12 5 18 7 24] You can also use nested where conditions in Python. Suppose you have data that you wish to compare. You can use the rely on np.where nested conditions and pandas dataframe to accomplish this with np.where 3 conditions or even more conditions.
🌐
Spark By {Examples}
sparkbyexamples.com › home › python › numpy where() multiple conditions
NumPy where() Multiple Conditions - Spark By {Examples}
March 27, 2024 - The condition arr > 5 checks for elements greater than 5, and arr % 2 == 0 checks for elements divisible by 2. The | operator represents logical OR, so it selects elements that satisfy either of these conditions. import numpy as np # Create ...
🌐
Medium
medium.com › @chanushkr › understanding-numpy-where-vs-pandas-dataframe-where-a-comprehensive-guide-457b5c0403c1
🧩 Understanding numpy.where vs. pandas.DataFrame.where: A Comprehensive Guide 🚀 | by Chanush KR | Medium
July 26, 2024 - Else, replace it with -1. This ... of DataFrames In contrast, pandas.DataFrame.where is used for conditional selection and replacement within Pandas DataFrames or Series....
🌐
NumPy
numpy.org › doc › stable › reference › generated › numpy.where.html
numpy.where — NumPy v2.5 Manual
An array with elements from x where condition is True, and elements from y elsewhere. ... Try it in your browser! >>> import numpy as np >>> a = np.arange(10) >>> a array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> np.where(a < 5, a, 10*a) array([ 0, 1, 2, 3, 4, 50, 60, 70, 80, 90])
🌐
Codecademy Forums
discuss.codecademy.com › projects › personal projects
Python : Using np.where() with conditions - Personal Projects - Codecademy Forums
January 13, 2022 - Hello! I am attempting to practice some hypothesis testing on a data set where one of the columns I’m trying to clean is gender. It looks like it was an open field where individuals were allowed to type in their gender. I am not too familiar with replacing 45+ different unique values, so I am making the task more manageable for myself by focusing on “Female”, “Male”, and anything else I’ll replace with “Other”. I have the following going on : `` import pandas as pd from matplotlib import pypl...
🌐
Medium
medium.com › @kelvinsang97 › python-np-where-97bdbdcf9eab
Python np.where(). This function can be used to select… | by Kelvin Kipsang | Medium
February 9, 2023 - The aggregation function is applied to the row values if and only the condition is false. This must sound very awkward, but that is how np.where function works. Now, we want to square the amount values if it is less than 100.
🌐
Note.nkmk.me
note.nkmk.me › home › python › pandas
pandas: Replace values based on conditions with where(), mask() | note.nkmk.me
January 17, 2024 - See the following articles for details. pandas: Extract rows that contain specific strings from a DataFrame · The np.where() function from NumPy can be used to replace values in DataFrame or Series according to conditions.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.0.4 documentation
Where the condition evaluates to True, the original values are retained; where it evaluates to False, values are replaced with corresponding entries from other. ... Where cond is True, keep the original value. Where False, replace with corresponding value from other.
🌐
Pandas
pandas.pydata.org › pandas-docs › version › 0.22 › generated › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 0.22.0 documentation
Enter search terms or a module, class or function name · Return an object of same shape as self and whose corresponding entries are from self where cond is True and otherwise are from other