Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy "where" with multiple conditions

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

like image 615
Poisson Avatar asked Aug 23 '16 19:08

Poisson


People also ask

Where multiple conditions numpy?

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.

Can NP where be nested?

We can use nested np. where() condition checks ( like we do for CASE THEN condition checking in other languages).

What does NP where () return?

The numpy. where() function returns the indices of elements in an input array where the given condition is satisfied.


1 Answers

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 
like image 118
Merlin Avatar answered Sep 23 '22 04:09

Merlin