0

When running the line

conditions = [
    (df['a']>=4 & df['a']<=7),
    (df['a']>=8 & df['a']<=15)
]

The compiler return

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

May I know why this happen?

import pandas as pd
import numpy as np
df=pd.DataFrame([1,3,6,2,4,10,4,5,15],columns=['a'])

conditions = [
    (df['a']>=4 & df['a']<=7),
    (df['a']>=8 & df['a']<=15)
]
#
choices = [1,2]
#
df['points'] = np.select(conditions, choices, default=0)
mpx
  • 3,081
  • 2
  • 26
  • 56
  • 1
    You lack the parentheses, use `((df['a']>=4) & (df['a']<=7))`. – Ynjxsjmh Apr 15 '22 at 13:02
  • This Question has already been answered on the below link in Staack Overflow: https://stackoverflow.com/questions/36921951/truth-value-of-a-series-is-ambiguous-use-a-empty-a-bool-a-item-a-any-o The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations. – Rehan Apr 15 '22 at 13:15

1 Answers1

1

Change your condition by adding ()

conditions = [
         (df['a']>=4) & (df['a']<=7),
         (df['a']>=8) & (df['a']<=15)
              ]

df['points'] = np.select(conditions, choices, default=0)
df
Out[329]: 
    a  points
0   1       0
1   3       0
2   6       1
3   2       0
4   4       1
5  10       2
6   4       1
7   5       1
8  15       2

Or with ge and le

conditions = [
    df['a'].ge(4) & df['a'].le(7),
    df['a'].ge(8) & df['a'].le(15)
]

And between

conditions = [
    df['a'].between(4,7),
    df['a'].between(8,15)
]
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Hi Beny, while at it, can you also add in your suggestion the `between`. I just found about this amazing function. `conditions = [ (df['a'].between(4, 7)), (df['a'].between(8, 15))]` . Cheers – mpx Apr 15 '22 at 13:17
  • 1
    @rpb added :-) ` – BENY Apr 15 '22 at 13:25