检查NA值并忽略它们

时间:2017-10-16 19:06:40

标签: python

我使用过try - except并且它有效,但我想知道我们是否可以使用for - if和else执行类似的功能。

# check for "NA" values, ignore the entries which have them
    # index: DeptTime = 4,Deptdelay = 15, Arrtime = 6, ArrDelay = 14
    for index in flight_data[1:]:
        try:
            x_dept.append(int(index[4]))
            y_dept.append(int(index[15]))
            x_arr.append(int(index[6]))
            y_arr.append(int(index[14])) 
        except ValueError:
            pass

例如,我们可以做这样的事情来获得完全相同的答案吗?

 # check for "NA" values, ignore the entries which have them
        # index: DeptTime = 4,Deptdelay = 15, Arrtime = 6, ArrDelay = 14
        for index in flight_data[1:]:
            if index[4] =='NA' or index[15] == 'NA' or index[14] =='NA' or index[6] == 'NA':
             pass
             else:
                x_dept.append(int(index[4]))
                y_dept.append(int(index[15]))
                x_arr.append(int(index[6]))
                y_arr.append(int(index[14])) 

0 个答案:

没有答案