How can I remove Nan from list Python/NumPy

前端 未结 10 1850
挽巷
挽巷 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:50

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

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

    Another way to do it would include using filter like this:

    countries = list(filter(lambda x: str(x) != 'nan', countries))
    
    0 讨论(0)
  • 2020-12-04 11:51

    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)
    
    0 讨论(0)
提交回复
热议问题