0

If I have a list or array of values X and I use the code:

X[X>=1]=1

I have a clean re-assignment of all values in X greater than or equal to 1 set equal to 1.

If I try the following:

X[X>0 and X<1] = 0.5

It ignores both and does nothing. Is there a way to have two inequalities enforced in a single line using this approach?

Aran-Fey
  • 35,525
  • 9
  • 94
  • 135

2 Answers2

0

Try:

X[(X>0) & (X<1)] = 0.5
Matias Eiletz
  • 390
  • 1
  • 12
  • It does indeed! What is the difference between "and" and "&" ? Thank you. – Amusing Automatons Mar 27 '19 at 11:52
  • @AmusingAutomatons `x and y` returns [`x if not x else y`](https://docs.python.org/3/library/stdtypes.html#boolean-operations-and-or-not). So the code you show in OP should've actually raised an error. `&` is just bitwise and. – a_guest Mar 27 '19 at 11:55
  • '&' is the syntax for "and" in Pandas. I appreciate if you rate my answer if it was useful for you – Matias Eiletz Mar 27 '19 at 16:38
0

This seems to work : X = [0.5 for i in X if (i>0) & (i<1)] edit : both & and and work on this.

Abhiram Satputé
  • 566
  • 1
  • 4
  • 19