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 OverflowPandas
pandas.pydata.org › docs › reference › api › pandas.DataFrame.where.html
pandas.DataFrame.where — pandas 3.0.6 documentation
Replace values where the condition is False.
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.
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
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
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
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
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': ...
Top answer 1 of 9
145
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
2 of 9
99
You can use a ternary:
np.where(consumption_energy > 400, 'high',
(np.where(consumption_energy < 200, 'low', 'medium')))
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 = ...
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(...)).
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.
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.
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...
Top answer 1 of 3
16
You should use nested np.where. It is like sql case clause. But be careful when there is nan in the data.
df=pd.DataFrame({'S':[1,1,2,2],'A':[1,0,1,0]})
df['Result'] = np.where((df.S == 1) & (df.A == 1), 1, #when... then
np.where((df.S == 1) & (df.A == 0), 0, #when... then
np.where((df.S == 2) & (df.A == 1), 0, #when... then
1))) #else
df
output:
| | S | A | Result |
|---|---|---|--------|
| 0 | 1 | 1 | 1 |
| 1 | 1 | 0 | 0 |
| 2 | 2 | 1 | 0 |
| 3 | 2 | 0 | 1 |
2 of 3
10
I would recommend using numpy.select if you have very nested operations.
df = pd.DataFrame({
"S": [1, 1, 2, 2],
"A": [1, 0, 1, 0]
})
# you could of course combine the clause (1, 4) and (2, 3) with the '|' or operator
df['RESULT'] = np.select([
(df.S == 1) & (df.A == 1),
(df.S == 1) & (df.A == 0),
(df.S == 2) & (df.A == 1),
(df.S == 2) & (df.A == 0)
], [1, 0, 0, 1])
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