How can I remove Nan from list Python/NumPy

前端 未结 10 1849
挽巷
挽巷 2020-12-04 10:56

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,

相关标签:
10条回答
  • 2020-12-04 11:24

    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]
    
    0 讨论(0)
  • 2020-12-04 11:32

    The question has changed, so to 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']
    
    0 讨论(0)
  • 2020-12-04 11:33

    if you check for the element type

    type(countries[1])
    

    the result will be <class float> so you can use the following code:

    [i for i in countries if type(i) is not float]
    
    0 讨论(0)
  • 2020-12-04 11:35

    In your example 'nan' is a string so instead of using isnan() just check for the string

    like this:

    cleanedList = [x for x in countries if x != 'nan']
    
    0 讨论(0)
  • 2020-12-04 11:45

    use numpy fancy indexing:

    In [29]: countries=np.asarray(countries)
    
    In [30]: countries[countries!='nan']
    Out[30]: 
    array(['USA', 'UK', 'France'], 
          dtype='|S6')
    
    0 讨论(0)
  • 2020-12-04 11:49
    import numpy as np
    
    mylist = [3, 4, 5, np.nan]
    l = [x for x in mylist if ~np.isnan(x)]
    

    This should remove all NaN. Of course, I assume that it is not a string here but actual NaN (np.nan).

    0 讨论(0)
提交回复
热议问题