2016-08-08 51 views
-1
df['class'] = np.where(((df['heart rate'] > 50) & (df['heart rate'] < 101)) & ((df['systolic blood pressure'] > 140 & df['systolic blood pressure'] < 160)) & 
    ((df['dyastolic blood pressure'] > 90 & ['dyastolic blood pressure'] < 100)) & ((df['temperature'] > 35 & df['temperature'] < 39)) & 
    ((df['respiratory rate'] >11 & df ['respiratory rate'] <19)) & ((df['pulse oximetry' > 95] & df['pulse oximetry' < 100])), "excellent", "critical") 

對於這個代碼,我得到:一個系列的真值是不確定

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

+3

對於此代碼我得到:噁心。 –

+0

你打算使用一個按位''而不是一個布爾'和'? – James

+0

'&'優先,你忘了一些括號。 – Julien

回答

0

也許這將解決你的(我們的)問題。

import random 
import pandas as pd 
import numpy as np 
heart_rate = [random.randrange(45, 125) for _ in range(500)] 
blood_pressure_systolic = [random.randrange(140, 230) for _ in range(500)] 
blood_pressure_dyastolic = [random.randrange(90, 140) for _ in range(500)] 
temperature = [random.randrange(34, 42) for _ in range(500)] 
respiratory_rate = [random.randrange(8, 35) for _ in range(500)] 
pulse_oximetry = [random.randrange(95, 100) for _ 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 
} 

vitalsign_maxima = { 
    'heart rate' : (50,101), 
    'systolic blood pressure' : (140,160), 
    'dyastolic blood pressure' : (90,100), 
    'temperature' : (35,39), 
    'respiratory rate' : (11,19), 
    'pulse oximetry' : (95,100) 
} 

def is_vitalsign_excellent(name): 
    lower, upper = vitalsign_maxima[name] 
    return (lower < df[name]) & (df[name] < upper) 

df = pd.DataFrame(vitalsign) 

f = np.where(is_vitalsign_excellent('heart rate') & 
      is_vitalsign_excellent('systolic blood pressure') & 
      is_vitalsign_excellent('dyastolic blood pressure') & 
      is_vitalsign_excellent('temperature') & 
      is_vitalsign_excellent('respiratory rate') & 
      is_vitalsign_excellent('pulse oximetry'),"excellent", "critical") 

這一個應該現在工作。

+0

我建議創建一個'dict',它與'df'字典具有相同的關鍵字,每個值都是優秀的最小/最大值作爲元組,所以您可以使您的支票更易於將來使用。 –

+0

我很抱歉地說,它不工作 –

+0

進口隨機 進口大熊貓作爲PD 進口numpy的作爲npheart_rate = [將random.randrange(45125)_範圍內(500)] blood_pressure_systolic = [random.randrange(140230)爲(500)] blood_pressure_dyastolic = [random.randrange(90,140)for _in range(500)] temperature = [random.randrange(34,42)for _in range(500)] respiratory_rate = [random (500)中的_randrange(8,35)] pulse_oximetry = [範圍(500)中的_的random.randrange(95,100)] –

相關問題