7

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!

swyx
  • 2,378
  • 5
  • 24
  • 39
  • 2
    Why are you using a one-row, 300-column DataFrame instead of a Series? – BrenBarn Dec 31 '14 at 06:36
  • hah yes i shouldve anticipated that question. i actually am iterating through >3000 rows of that dataframe, however each calculation depends on results from prior rows, hence have to go one row at a time. I know about the importance and speed of vectorizing but prioritize just getting the answer over that (which I also seem to be unsuccessful at...) – swyx Dec 31 '14 at 06:44
  • the clause `sign(x) != 0` is unnecessary. – acushner Dec 31 '14 at 14:29

2 Answers2

16

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)
gboffi
  • 22,939
  • 8
  • 54
  • 85
  • 2
    It took you quite a while to recognize the absolute greatness of my answer but finally you accepted it! … Seriously: thank you for taking your time to go back and tick this answer of mine... – gboffi May 25 '16 at 08:49
  • haha i was not a very active SO user back then. since i did it again with my response to your comment. i wonder how long we can keep this up – swyx Jun 19 '17 at 14:39
4

Similar to the post by @gboffi, but more centralized for your original request according to my understanding try:

x = np.where(np.logical_and((x*y) < 0, x != 0))

or

x = np.where(((x*y) < 0) & (x != 0)))
alacy
  • 4,972
  • 8
  • 30
  • 47