Python 使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38834028/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use a.empty, a.bool(), a.item(), a.any() or a.all()
提问by Shamsul Masum
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"]
it seems to be that,
似乎是这样,
error that i am receiving :
我收到的错误:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()
ValueError:系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 a.all()
. how can i sort it out
. 我该如何解决
采纳答案by ayhan
As user2357112 mentioned in the comments, you cannot use chained comparisons here. For elementwise comparison you need to use &
. That also requires using parentheses so that &
wouldn't take precedence.
正如评论中提到的 user2357112,您不能在此处使用链式比较。对于元素比较,您需要使用&
. 这也需要使用括号,这样&
就不会优先了。
It would go something like this:
它会是这样的:
mask = ((50 < df['heart rate']) & (101 > df['heart rate']) & (140 < df['systolic...
In order to avoid that, you can build series for lower and 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 you can slice it as 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 for assignment you can use np.where:
对于分配,您可以使用 np.where:
df['class'] = np.where(mask, 'excellent', 'critical')
回答by ??????
solution is easy:
解决方案很简单:
replace
代替
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")
by
经过
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")