I have a list that countain values, one of the values I got is \'nan\'
countries= [nan, \'USA\', \'UK\', \'France\']
I tried to remove it,
The problem comes from the fact that np.isnan()
does not handle string values correctly. For example, if you do:
np.isnan("A")
TypeError: ufunc 'isnan' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''
However the pandas version pd.isnull()
works for numeric and string values:
pd.isnull("A")
> False
pd.isnull(3)
> False
pd.isnull(np.nan)
> True
pd.isnull(None)
> True
I like to remove missing values from a list like this:
list_no_nan = [x for x in list_with_nan if pd.notnull(x)]
Another way to do it would include using filter like this:
countries = list(filter(lambda x: str(x) != 'nan', countries))
I noticed that Pandas for example will return 'nan' for blank values. Since it's not a string you need to convert it to one in order to match it. For example:
ulist = df.column1.unique() #create a list from a column with Pandas which
for loc in ulist:
loc = str(loc) #here 'nan' is converted to a string to compare with if
if loc != 'nan':
print(loc)