比较Python中的浮点值

2024-05-21 12:35:07 发布

您现在位置: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<=得分<;6然后我向该行(类编号)添加一列,使其等于1。如果在范围6<=得分<;7.5则类别编号为2,如果在7.5<=得分<;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中的浮点比较是否缺少一些东西?任何帮助都将不胜感激


Tags: oftheinltnumberdatadateclass
2条回答

尝试使用序列的apply方法,并将结果分配给名为Class Number的新列

在您的情况下,它将类似于:

data = pd.DataFrame({'CVSS Score': [1, 2, .5, 6.2, 6.3, 9, 19, 6.1, 2, .5]})

def classify_cvss_score(score):
    if 0 < score < 6:
        return 1
    elif 6 <= score <= 7.5:
        return 2
   
    return 3

data['Class Number'] = data['CVSS Score'].apply(classify_cvss_score)

您的问题不是比较浮动,而是在分配时覆盖了数据帧的整个列

您只需要设置满足条件的行,请参见 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 

相关问题 更多 >