2016-08-08 14 views
-1
import random 
import pandas as pd 

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} 


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"] 

它似乎是,使用a.empty,a.bool(),a.item(),a.any()或a.all()

錯誤我收到: ValueError:一個Series的真值不明確。使用a.empty,a.bool(),a.item(),a.any()或a.all()。我怎麼可以排序出來

+0

哪行代碼導致該錯誤?這個問題與比較類似字典或列表的東西與字符串,char,int等東西有關,因爲這些值無法進行比較。 – BrandonM

+1

您不能使用'a user2357112

回答

2

正如在評論中提到user2357112,你不能在這裏使用鏈比較。對於元素比較,您需要使用&。這還需要使用括號,以便&不會優先。

它會去是這樣的:

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

爲了避免這種情況,你可以建立下限和上限系列:

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) 

現在,您可以按如下切它:

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 

而對於作業,您可以使用np.where:

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

只是爲了讓你知道第一個解決方案沒有工作,但第二個解決方案確實解決了我的問題。非常感謝幫助像我這樣的人。 –

+0

@ShamsulMasum如果你像在問題中那樣添加',「優秀」,「關鍵」)到最後,它將無法工作。它應該只包含條件。然後你可以使用np.where。 – ayhan

+0

如果我需要添加更多課程選項,例如這裏我只有兩個(優秀和關鍵)選項。 –

0

的解決方案很簡單:

更換

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") 

通過

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") 
相關問題