使用a.empty,a.bool(),a.item(),a.any()或a.all()

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

似乎是这样的,

我收到的错误信息:

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

如何解决它?


哪一行代码导致了这个错误?问题与将类似于字典或列表的东西与字符串、字符、整数等进行比较有关,因为这些值不能进行比较。 - BrandonM
4
在pandas中不能使用a < b < c的链式比较,原因与你需要使用&代替and一样。 - user2357112
@cs95 所指出的重复只是部分重复。因为该重复项未使用链式比较。上述代码存在两个问题,而该重复项仅回答了其中一个问题。 - 9769953
2个回答

8

正如评论中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')

只是想让你知道,第一个解决方案没有起作用,但第二个解决方案确实解决了我的问题。非常感谢并祝愿你帮助像我这样的其他人时好运。 - Shamsul Masum
如果你像问题中所做的那样在末尾添加 , "excellent", "critical"),它将不起作用。它应该只包含条件。然后你可以使用 np.where。 - ayhan
如果我需要添加更多的类选项,比如这里我只有两个(优秀和关键),该怎么办? - Shamsul Masum
你能为此发布一个示例吗? - ayhan

-1

解决方案很简单:

替换

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接