samedi 17 juin 2017

How to clean NaN and Inf in list type data in Python

import math 
raw_data = [56.2, float('NaN'), 51.7, 
    55.3, 52.5, float('Nan'), 47.8, float('Inf'), float('-Inf')]

print(raw_data)

filtered_data = [] 
for v in raw_data:
    if not ((v is float('NaN')) | (v is float('Inf')) | (v is float('-Inf'))):          filtered_data.append(v)

print(filtered_data)

print(raw_data[1]) print(raw_data[1] is float('NaN'))

I try to clean the NaN, Inf and -Inf in the list data. The if condition seems to take no effect.

raw_data[1] does be nan. Why print(raw_data[1] is float('NaN')) is False?

Aucun commentaire:

Enregistrer un commentaire