如何从列表Python/NumPy中删除Nan

use*_*937 67 python numpy

我有一个列出值的列表,我得到的值之一是'nan'

countries= [nan, 'USA', 'UK', 'France']
Run Code Online (Sandbox Code Playgroud)

我试图删除它,但我每次都会收到错误

cleanedList = [x for x in countries if (math.isnan(x) == True)]
TypeError: a float is required
Run Code Online (Sandbox Code Playgroud)

当我尝试这个时:

cleanedList = cities[np.logical_not(np.isnan(countries))]
cleanedList = cities[~np.isnan(countries)]

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''
Run Code Online (Sandbox Code Playgroud)

小智 93

问题已经改变,所以有答案:

无法使用字符串进行测试,math.isnan因为这需要一个float参数.在countries列表中,您有浮点数和字符串.

在您的情况下,以下内容应该足够:

cleanedList = [x for x in countries if str(x) != 'nan']
Run Code Online (Sandbox Code Playgroud)

老答案

在你的countries列表中,文字'nan'是一个字符串而不是Python浮点数nan,相当于:

float('NaN')
Run Code Online (Sandbox Code Playgroud)

在您的情况下,以下内容应该足够:

cleanedList = [x for x in countries if x != 'nan']
Run Code Online (Sandbox Code Playgroud)

  • 张绍臣:它不是字符串,是浮点数。仔细查看更新后的答案;Lego Stormtroopr将x转换为字符串,以便您可以进行比较。即使与`nan`相比,`nan`对于'==`总是返回false,因此这是比较它的最简单方法。 (2认同)

Yoh*_*dia 13

问题来自于np.isnan()没有正确处理字符串值的事实.例如,如果你这样做:

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''
Run Code Online (Sandbox Code Playgroud)

但是,pandas版本pd.isnull()适用于数字和字符串值:

pd.isnull("A")
> False

pd.isnull(3)
> False

pd.isnull(np.nan)
> True

pd.isnull(None)
> True
Run Code Online (Sandbox Code Playgroud)


Aja*_*hah 11

导入numpy为np

import numpy as np

mylist = [3, 4, 5, np.nan]
l = [x for x in mylist if ~np.isnan(x)]
Run Code Online (Sandbox Code Playgroud)

这应该删除所有NaN.当然,我认为它不是一个字符串,而是实际的NaN.

  • 这给了我错误:TypeError: ufunc 'isnan' not supported for the input types,并且输入不能根据转换规则 ''safe'' 安全地强制转换为任何受支持的类型 (3认同)

Aar*_*and 10

我喜欢从这样的列表中删除缺失值:

list_no_nan = [x for x in list_with_nan if pd.notnull(x)]
Run Code Online (Sandbox Code Playgroud)


小智 6

如果您检查元素类型

type(countries[1])
Run Code Online (Sandbox Code Playgroud)

结果将是<class float> 这样您就可以使用以下代码:

[i for i in countries if type(i) is not float]
Run Code Online (Sandbox Code Playgroud)


zha*_*hen 5

使用numpy 花式索引:

In [29]: countries=np.asarray(countries)

In [30]: countries[countries!='nan']
Out[30]: 
array(['USA', 'UK', 'France'], 
      dtype='|S6')
Run Code Online (Sandbox Code Playgroud)


vlm*_*ado 5

使用您的示例,其中...

countries= [nan, 'USA', 'UK', 'France']

由于nan不等于nan(nan!= nan)且country [0] = nan,因此应注意以下几点:

countries[0] == countries[0]
False
Run Code Online (Sandbox Code Playgroud)

然而,

countries[1] == countries[1]
True
countries[2] == countries[2]
True
countries[3] == countries[3]
True
Run Code Online (Sandbox Code Playgroud)

因此,以下应工作:

cleanedList = [x for x in countries if x == x]
Run Code Online (Sandbox Code Playgroud)

  • 当字符串列表中有 float('nan') 时,这是唯一有效的答案 (3认同)

小智 5

直接去除nan值的一种方法是:

import numpy as np    
countries.remove(np.nan)
Run Code Online (Sandbox Code Playgroud)