To remove NaN values from a NumPy array x:
x = x[~numpy.isnan(x)]
Explanation
The inner function numpy.isnan returns a boolean/logical array which has the value True everywhere that x is not-a-number. Since we want the opposite, we use the logical-not operator ~ to get an array with Trues everywhere that x is a valid number.
Lastly, we use this logical array to index into the original array x, in order to retrieve just the non-NaN values.
To remove NaN values from a NumPy array x:
x = x[~numpy.isnan(x)]
Explanation
The inner function numpy.isnan returns a boolean/logical array which has the value True everywhere that x is not-a-number. Since we want the opposite, we use the logical-not operator ~ to get an array with Trues everywhere that x is a valid number.
Lastly, we use this logical array to index into the original array x, in order to retrieve just the non-NaN values.
filter(lambda v: v==v, x)
works both for lists and numpy array since v!=v only for NaN
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']
Using your example where...
countries= [nan, 'USA', 'UK', 'France']
Since nan is not equal to nan (nan != nan) and countries[0] = nan, you should observe the following:
countries[0] == countries[0]
False
However,
countries[1] == countries[1]
True
countries[2] == countries[2]
True
countries[3] == countries[3]
True
Therefore, the following should work:
cleanedList = [x for x in countries if x == x]
This is probably because the np.isnan() is failing to deal with string types among your possible element types in collection. You could try to use panda's isnull() to remove NaN values.
import pandas as pa
import numpy as np
a = ['A', np.nan, np.nan, 1.67, 8]
a = [x for x in a if not pa.isnull(x)]
print(a)
There's function in pandas called pandas.DataFrame.dropna which drops all the columns with nan values. Just create DataFrame if you didn't, and do df.dropna().
You can just replace NaN and infinite values with the following mask:
output[~np.isfinite(output)] = 0
>>> output
array([[1. , 0.5 , 1. , 1. , 0. ,
1. ],
[1. , 1. , 0.5 , 1. , 0.46524064,
1. ],
[1. , 1. , 1. , 0. , 1. ,
1. ]])
There is a special function just for that:
numpy.nan_to_num(x_arr, copy=False, nan=0.0, posinf=0.0, neginf=0.0)