hi folks i have look all over SO and google and cant find anything similar...
I have a dataframe x (essentially consisting of one row and 300 columns) and another dataframe y with same size but different data. I would like to modify x such that it is 0 if it has a different sign to y AND x itself is not 0, else leave it as it is. so this requires the use of np.where with multiple conditions. However the multiple condition examples i've seen all use scalars, and when i use the same syntax it does not seem to work (ends up setting -everything- to zero, no error). i'm worried about assign-by-reference issues hidden somewhere or other (y is x after shifting but as far as i can tell there is no upstream issue above this code) any ideas?
the code i am trying to debug is:
tradesmade[i:i+1] = np.where((sign(x) != sign(y)) & (sign(x) != 0), 0, x)
which just returns a bunch of zeros. I have also tried
tradesmade[i:i+1][(sign(x) != sign(y)) * (sign(x) != 0)] = 0
but this does not seem to work either. I have been at this for hours and am at a total loss. please help!
Python NumPy where() is used to get an array with selected elements from the existing array by checking single or multiple conditions. It returns the indices of the array for with each condition being True. Using & Operator, | Operator, NumPy. logical_and() and, numpy.
Using Loc to Filter With Multiple Conditions The loc function in pandas can be used to access groups of rows or columns by label. Add each condition you want to be included in the filtered result and concatenate them with the & operator. You'll see our code sample will return a pd. dataframe of our filtered rows.
We can use nested np. where() condition checks ( like we do for CASE THEN condition checking in other languages).
It is not clear to me what you exactly want to do when a y
element is equal to zero... anyway the key point in this answer is "use np.logical_{and,not,or,xor}
functions".
I think that the following, albeit formulated differently from your example, does what you want, but if I'm wrong you should be able to combine different tests to achieve what you want,
x = np.where(np.logical_or(x*y>0, y==0), x, 0)
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