'Getting Value Error when assigning row based on multiple condition in Pandas [duplicate]

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)


Solution 1:[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)
]

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1