Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete numpy nan from a list of strings in Python?

I have a list of strings

x = ['A', 'B', nan, 'D']

and want to remove the nan.

I tried:

x = x[~numpy.isnan(x)]

But that only works if it contains numbers. How do we solve this for strings in Python 3+?

like image 210
JohnAndrews Avatar asked Mar 23 '17 15:03

JohnAndrews


1 Answers

If you have a numpy array you can simply check the item is not the string nan, but if you have a list you can check the identity with is and np.nan since it's a singleton object.

In [25]: x = np.array(['A', 'B', np.nan, 'D'])

In [26]: x
Out[26]: 
array(['A', 'B', 'nan', 'D'], 
      dtype='<U3')

In [27]: x[x != 'nan']
Out[27]: 
array(['A', 'B', 'D'], 
      dtype='<U3')


In [28]: x = ['A', 'B', np.nan, 'D']

In [30]: [i for i in x if i is not np.nan]
Out[30]: ['A', 'B', 'D']

Or as a functional approach in case you have a python list:

In [34]: from operator import is_not

In [35]: from functools import partial

In [37]: f = partial(is_not, np.nan)

In [38]: x = ['A', 'B', np.nan, 'D']

In [39]: list(filter(f, x))
Out[39]: ['A', 'B', 'D']
like image 139
Mazdak Avatar answered Sep 29 '22 01:09

Mazdak