If I have a dataframe df with column x and want to create column y based on values of x using this in pseudo code
if df['x'] <-2 then df['y'] = 1
else if df['x'] > 2 then df['y']= -1
else df['y'] = 0
how would I achieve this. I assume np.where is the best way to do this but not sure how to code it correctly.
One simple method would be to assign the default value first and then perform 2
loc
calls:If you wanted to use
np.where
then you could do it with a nestednp.where
:So here we define the first condition as where x is less than -2, return 1, then we have another
np.where
which tests the other condition where x is greater than 2 and returns -1, otherwise return 0timings
So for this sample dataset the
np.where
method is twice as fast