-1

I'm new to Pandas, the answer may be obvious.

I have 3 series of the same length: a, b, c

a[b > c] = 0

works, but:

a[math.fabs(b) > c] = 0

doesn't work, and

a[(b > c or b < -c)] = 0

doesn't work either.

How can I implement that logic?

Thomas
  • 10,933
  • 14
  • 65
  • 136
  • `a[(b>c) | (b<-c)]` or `a[b.abs().gt(c)]`? Make sure that `a,b,c` are not just equal length, they should have same indexing as well. – Quang Hoang Jul 22 '19 at 20:33
  • Please have a look at [How to create good pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) and provide a [mcve] for your issue so that we can provide more specific help – G. Anderson Jul 22 '19 at 20:34
  • What do you mean by "it doesn't work"? Do you get an error? Do you get unexpected results? – John Gordon Jul 22 '19 at 20:36
  • 1
    On your third example, see [this answer](https://stackoverflow.com/questions/21415661/logical-operators-for-boolean-indexing-in-pandas). – Brendan Jul 22 '19 at 20:51
  • @JohnGordon, sorry I should have written the error: : (, ValueError('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().'), None) – Thomas Jul 22 '19 at 21:39

1 Answers1

-1

Your issue is that in the first expression the expression you use is vectorized while in the other one it is not.

  • In the first expression, the < operation between two series returns a series as well
  • In the second expression, math.fabs is supposed to be applied elements by elements and not to an array/series of elements (try the numpy version instead is it exists).
  • In the third expression, the or operation is not vectorized and you should use | instead.
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
Robin Nicole
  • 646
  • 4
  • 17