python - Use a.empty, a.bool(), a.item(), a.any() or a.all() -


import random import pandas pd  heart_rate = [random.randrange(45,125) _ in range(500)] blood_pressure_systolic = [random.randrange(140,230) _ in range(500)] blood_pressure_dyastolic = [random.randrange(90,140) _ in range(500)] temperature = [random.randrange(34,42) _ in range(500)] respiratory_rate = [random.randrange(8,35) _ in range(500)] pulse_oximetry = [random.randrange(95,100) _ in range(500)]   vitalsign = {'heart rate' : heart_rate,              'systolic blood pressure' : blood_pressure_systolic,              'dyastolic blood pressure' : blood_pressure_dyastolic,              'temperature' : temperature,              'respiratory rate' : respiratory_rate,              'pulse oximetry' : pulse_oximetry}   df = pd.dataframe(vitalsign)   df.to_csv('vitalsign.csv')   mask = (50  < df['heart rate'] < 101 &         140 < df['systolic blood pressure'] < 160 &         90  < df['dyastolic blood pressure'] < 100 &         35  < df['temperature'] < 39 &         11  < df['respiratory rate'] < 19 &         95  < df['pulse oximetry'] < 100         , "excellent", "critical")  df.loc[mask, "class"] 

it seems that,

error receiving : valueerror: truth value of series ambiguous. use a.empty, a.bool(), a.item(), a.any() or a.all(). how can sort out

as user2357112 mentioned in comments, cannot use chained comparisons here. elementwise comparison need use &. requires using parentheses & wouldn't take precedence.

it go this:

mask = ((50  < df['heart rate']) & (101 > df['heart rate']) & (140 < df['systolic... 

in order avoid that, can build series lower , upper limits:

low_limit = pd.series([90, 50, 95, 11, 140, 35], index=df.columns) high_limit = pd.series([160, 101, 100, 19, 160, 39], index=df.columns) 

now can slice follows:

mask = ((df < high_limit) & (df > low_limit)).all(axis=1) df[mask] out:       dyastolic blood pressure  heart rate  pulse oximetry  respiratory rate  \ 17                        136          62              97                15    69                        110          85              96                18    72                        105          85              97                16    161                       126          57              99                16    286                       127          84              99                12    435                        92          67              96                13    499                       110          66              97                15          systolic blood pressure  temperature   17                       141           37   69                       155           38   72                       154           36   161                      153           36   286                      156           37   435                      155           36   499                      149           36   

and assignment can use np.where:

df['class'] = np.where(mask, 'excellent', 'critical') 

Comments