[SOLVED] How can I remove Nan from list Python/NumPy

Issue

This Content is from Stack Overflow. Question asked by Brian Smith

I have below list

import pandas as pd
import numpy as np
dat = pd.DataFrame({'name' : ['A', 'C', 'A', 'B', 'C'], 'val' : [1,2,1,2,4]})
List = [dat, np.nan, dat, np.nan, np.nan]
List

I want to retain only those elements where they are not nan.

There is a similar discussion in How can I remove Nan from list Python/NumPy, but it is only applicable if all elements are scalar.

Is there any way to remove nan if some elements are matrix/dataframe?



Solution

The question has changed, so too has the answer:

Strings can’t be tested using math.isnan as this expects a float argument. In your countries list, you have floats and strings.

In your case the following should suffice:

cleanedList = [x for x in countries if str(x) != 'nan']

Old answer

In your countries list, the literal 'nan' is a string not the Python float nan which is equivalent to:

float('NaN')

In your case the following should suffice:

cleanedList = [x for x in countries if x != 'nan']


This Question was asked in StackOverflow by user3001937 and Answered by user764357 It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?