I try to add a new column "energy_class" to a dataframe "df_energy" which it contains the string "high" if the "consumption_energy" value > 400, "medium" if the "consumption_energy" value is between 200 and 400, and "low" if the "consumption_energy" value is under 200. I try to use np.where from numpy, but I see that numpy.where(condition[, x, y])
treat only two condition not 3 like in my case.
Any idea to help me please?
Thank you in advance
We can specify multiple conditions inside the numpy. where() function by enclosing each condition inside a pair of parenthesis and using a | operator between them. In the above code, we selected the values from the array of integers values that are either greater than 2 or completely divisible by 2 with the np.
We can use nested np. where() condition checks ( like we do for CASE THEN condition checking in other languages).
The numpy. where() function returns the indices of elements in an input array where the given condition is satisfied.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With