I am trying to do a groupby in a Pandas dataframe (column Mode) followed by an apply that sums the values in column Sensor Glucose (mg/dL) for rows where the isInRange function returns True.
For some reason, I receive:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Yet if I replace the function with something more simple like x[x['Sensor Glucose (mg/dL)']>160]['Sensor Glucose (mg/dL)'].sum(), it works fine.  Not sure why it isn't applying this function as expected.
DataFrame Info
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 55343 entries, 0 to 55342
Data columns (total 6 columns):
 #   Column                  Non-Null Count  Dtype         
---  ------                  --------------  -----         
 0   Date                    55343 non-null  object        
 1   Time                    55343 non-null  object        
 2   Sensor Glucose (mg/dL)  55343 non-null  float64       
 3   DateTime                55343 non-null  datetime64[ns]
 4   Mode                    55343 non-null  object        
 5   ResultRange             55343 non-null  object        
dtypes: datetime64[ns](1), float64(1), object(4)
memory usage: 2.5+ MB
isInRange function
def isInRange(cgmValue, rangeName):
    if cgmValue > 250:
        return rangeName == 'hyperglycemia-critical'
    elif cgmValue > 180:
        return rangeName == 'hyperglycemia'
    elif cgmValue >= 70:
        if (cgmValue <= 150):
            return rangeName == 'cgm70:180' or rangeName == 'cgm70:150'
        else:
            return rangeName == 'cgm70:180'
    elif cgmValue >= 54:
        return rangeName == 'hypoglycemia-level1'
    else:
        return rangeName == 'hypoglycemia-level2' 
GroupBy/Apply
Result = CGM.groupby('Mode').apply(lambda x: x[isInRange(x['Sensor Glucose (mg/dL)'],'hyperglycemia-critical') == True]['Sensor Glucose (mg/dL)'].sum())
 
     
    