Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing nan from list - Python

Tags:

python

pandas

I am trying to remove 'nan' from a list, but it is refusing to go. I have tried both np.nan and 'nan'.

This is my code:

ztt = []
for i in z:
    if i != 'nan':
        ztt.append(i) 
ztt

or:

ztt = []
for i in z:
    if i != np.nan:
        ztt.append(i) 
ztt

I am still getting the output:

[[46.0, 34.0, 32.0, 40.0, 34.0, 29.0, 38.0, 39.0, 45.0, 32.0, 28.0, 43.0],
 [32.0, 30.0, 67.0, 66.0, 28.0, 19.0, 39.0, 32.0, 51.0, 28.0, 20.0, 36.0],
 [29.0, 24.0, 37.0, 31.0, 32.0, 34.0, 28.0, 31.0, 28.0, 33.0, 28.0, 39.0],
 [27.0, 29.0, 35.0, nan, nan, nan, nan, nan, nan, nan, nan, nan]]

Anyone know what is going wrong?

like image 471
ScoutEU Avatar asked Jul 28 '17 13:07

ScoutEU


1 Answers

for i in z:
    if not math.isnan(i):
        ztt.append(i)
like image 106
Dawid Fieluba Avatar answered Sep 22 '22 10:09

Dawid Fieluba