Eric Enkele
Eric Enkele

Reputation: 183

Removing nan values from a Python List

Can someone see why this is not working? I am trying to remove nan values from my python List/array.

import math
import numpy as np

def clean_List_nan(List):
    Myarray=np.array(List)
    x = float('nan')
    for elem in Myarray:
        if math.isnan(x):
            x = 0.0
    return Myarray


oldlist =[nan, 19523.3211203121, 19738.4276377355, 19654.8478302742, 119.636737571360, 19712.4329437810, nan, 20052.3645613346, 19846.4815936009, 20041.8676619438, 19921.8126944154, nan, 20030.5073635719]

print(clean_List_nan(oldlist))

Upvotes: 3

Views: 6694

Answers (3)

Anonymouse
Anonymouse

Reputation: 1

I use a list comprehension:

ax_graph_ymin_list = [x for x in ax_graph_ymin_list if np.isnan(x) != True]

meaning: "exchange x for x in the list if np.isnan() function does not return a True value"

Upvotes: 0

fafl
fafl

Reputation: 7385

The answer from Mitch is probably the best way to do it. If you wish to write this manually you can do something like

cleanlist = [0.0 if math.isnan(x) else x for x in oldlist]

Upvotes: 2

miradulo
miradulo

Reputation: 29680

The control flow in your function makes no sense - you set a variable x to be nan, and then check if it is indeed nan in your loop and set it to 0. You never touch nor check any of the elements of the array.

To properly convert your nan values to 0, you could simply use numpy.nan_to_num as it appears you're working with NumPy arrays.

Demo

In[37]: arr
Out[37]: 
array([            nan,  19523.32112031,  19738.42763774,  19654.84783027,
          119.63673757,  19712.43294378,             nan,  20052.36456133,
        19846.4815936 ,  20041.86766194,  19921.81269442,             nan,
        20030.50736357])

In[38]: np.nan_to_num(arr)
Out[38]: 
array([     0.        ,  19523.32112031,  19738.42763774,  19654.84783027,
          119.63673757,  19712.43294378,      0.        ,  20052.36456133,
        19846.4815936 ,  20041.86766194,  19921.81269442,      0.        ,
        20030.50736357])

If you're more interested in having a functioning version of an approach for a regular Python list, you might try something like this, or a list comprehension as fafl has provided.

In[39]: list(map(lambda x: 0.0 if math.isnan(x) else x, oldlist))
Out[39]: 
[0.0,
 19523.3211203121,
 19738.4276377355,
 19654.8478302742,
 119.63673757136,
 19712.432943781,
 0.0,
 20052.3645613346,
 19846.4815936009,
 20041.8676619438,
 19921.8126944154,
 0.0,
 20030.5073635719]

Upvotes: 7

Related Questions