1

I am receiving below error message in Python after this line of code: data is the panda data frame and A, B are numbers.

if((data.loc[ii]>=A) & (data.loc[ii]<B)):
                labels[ii]=C

Error :The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

I have read everything in the internet it says use & instead of and and | instead of or, I even use the parenthesis to avoid operator precedence rule but I am wondering why I am still getting the above error since I am using correct logical operator ?.

Thanks

justin
  • 173
  • 2
  • 3
  • 11
  • What's the value of the conditional (ie. without the `if`)? – hpaulj Jun 22 '18 at 00:06
  • I just tried for different values and without if, sometimes I get true and sometimes false depending on which elements of data is accessed – justin Jun 22 '18 at 00:09
  • 1
    The return of `(...)>=A & (...)= A` and `< B`, then you do your code `(..).any()`which would return `True`or`False`. Now, if you wanna know if *all* elements in your series are indeed `>=A` or ` – rafaelc Jun 22 '18 at 00:10
  • 1
    `numpy` raises this error when a boolean array with more than one element occurs in an `if` context (or other cases that expect a scalar boolean). In other words, `np.array([True, False, True, True])` is ambiguous. – hpaulj Jun 22 '18 at 00:23
  • @hpaulj None of the answers in other posts were useful for me – justin Jun 22 '18 at 00:51

0 Answers0