python - 比较 Python 中的浮点数
问题描述
我在比较 python 中的条件语句中的浮点数时遇到问题。我有一个如下所示的数据集:
CVE-ID CVE Creation Date Patch Date CVSS Score \
0 CVE-2012-6702 6/3/2016 6/7/2016 5.9
1 CVE-2015-8951 8/15/2016 12/16/2015 7.8
2 CVE-2015-9016 3/28/2017 8/15/2015 7.0
3 CVE-2016-10230 3/1/2017 11/28/2016 9.8
4 CVE-2016-10231 3/1/2017 12/14/2016 7.8
Bug Description # of lines added \
0 Expat, when used in a parser that has not call... 41
1 Multiple use-after-free vulnerabilities in sou... 10
2 In blk_mq_tag_to_rq in blk-mq.c in the upstrea... 3
3 A remote code execution vulnerability in the Q... 7
4 An elevation of privilege vulnerability in the... 8
number of lines removed Vuln Type Diff of dates
0 7 UNK 4
1 3 #NAME? -243
2 1 UNK -591
3 0 Exec Code -93
4 0 UNK -77
我想要完成的是循环遍历 CVSS 分数(类型 float),如果它在 0<=score<6 范围内,那么我在该行(类号)中添加一列并使其等于 1。如果它在 6<=score<7.5 范围内,则班级编号为 2,如果在 7.5<=score<10 范围内,则班级编号为 3。如果正确完成,这应该是这样的:
CVE-ID CVE Creation Date Patch Date CVSS Score \
0 CVE-2012-6702 6/3/2016 6/7/2016 5.9
1 CVE-2015-8951 8/15/2016 12/16/2015 7.8
2 CVE-2015-9016 3/28/2017 8/15/2015 7.0
3 CVE-2016-10230 3/1/2017 11/28/2016 9.8
4 CVE-2016-10231 3/1/2017 12/14/2016 7.8
Bug Description # of lines added \
0 Expat, when used in a parser that has not call... 41
1 Multiple use-after-free vulnerabilities in sou... 10
2 In blk_mq_tag_to_rq in blk-mq.c in the upstrea... 3
3 A remote code execution vulnerability in the Q... 7
4 An elevation of privilege vulnerability in the... 8
number of lines removed Vuln Type Diff of dates Class Number
0 7 UNK 4 1
1 3 #NAME? -243 3
2 1 UNK -591 2
3 0 Exec Code -93 3
4 0 UNK -77 3
我的代码现在看起来像这样:
data = pd.read_csv('tag_SA.txt', sep='|')
for score in data['CVSS Score']:
if 0.0 < score < 6.0:
data["Class Number"] = 1
elif(6 <= score < 7.5):
data["Class Number"] = 2
else:
data["Class Number"] = 3
我得到的输出是这样的:
CVE-ID CVE Creation Date Patch Date CVSS Score \
0 CVE-2012-6702 6/3/2016 6/7/2016 5.9
1 CVE-2015-8951 8/15/2016 12/16/2015 7.8
2 CVE-2015-9016 3/28/2017 8/15/2015 7.0
3 CVE-2016-10230 3/1/2017 11/28/2016 9.8
4 CVE-2016-10231 3/1/2017 12/14/2016 7.8
Bug Description # of lines added \
0 Expat, when used in a parser that has not call... 41
1 Multiple use-after-free vulnerabilities in sou... 10
2 In blk_mq_tag_to_rq in blk-mq.c in the upstrea... 3
3 A remote code execution vulnerability in the Q... 7
4 An elevation of privilege vulnerability in the... 8
number of lines removed Vuln Type Diff of dates Class Number
0 7 UNK 4 3
1 3 #NAME? -243 3
2 1 UNK -591 3
3 0 Exec Code -93 3
4 0 UNK -77 3
所以它只是去 else 语句并认为其他语句是错误的。python中的浮点比较有什么我缺少的吗?任何帮助,将不胜感激
解决方案
您的问题不在于比较浮点数,而是您在分配时覆盖了数据框的整个列。
您只需要设置满足条件的那些行,请参阅 https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html,您可能还应该查看https://pandas.pydata .org/pandas-docs/stable/user_guide/10min.html。
使用那里记录的内容:
data = pd.read_csv('tag_SA.txt', sep='|')
data['Class Number'] = 3
mask = (0.0 < data['CVSS Score']) & (data['CVSS Score'] <= 6.0)
data.loc[mask, 'Class Number'] = 1
mask = (6.0 < data['CVSS Score']) & (data['CVSS Score'] <= 7.5)
data.loc[mask, 'Class Number'] = 2
你也可以pandas.cut
这样使用:
max_val = data['CVSS Score'].max()
# codes start at 0, add 1 if needed
data['Class Number'] = pd.cut(data['CVSS Score'], [0, 6, 7.5, max_val]).codes + 1
推荐阅读
- tensorflow - 为什么我使用 Keras 与随机森林或 knn 的结果很差?
- python - 在 Windows 上运行时,如何强制 pandas 1.0.3 编写编码为 UTF-8 的 csv?
- postgresql - postgresql 在 SELECT 中的一列中应用不同的
- flutter - 做什么
颤抖的意思? - listview - 打印时列表值正确,但使用时列表为空 Flutter
- sql - 重写 Oracle SQL 自联接查询
- wordpress - WordPress 子域
- ios - 如何在注销时快速清空数组
- css - 相对于视口的右边缘平移元素
- python - 在 Jupyter 中导出多行 SQL