如何从 Python/NumPy 列表中删除 Nan
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21011777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I remove Nan from list Python/NumPy
提问by user3001937
I have a list that countain values, one of the values I got is 'nan'
我有一个包含值的列表,我得到的值之一是“nan”
countries= [nan, 'USA', 'UK', 'France']
I tried to remove it, but I everytime get an error
我试图删除它,但我每次都收到错误
cleanedList = [x for x in countries if (math.isnan(x) == True)]
TypeError: a float is required
When I tried this one :
当我尝试这个时:
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''
回答by user3001937
The question has changed, so to has the answer:
问题变了,所以有了答案:
Strings can't be tested using math.isnanas this expects a float argument. In your countrieslist, you have floats and strings.
不能使用字符串进行测试,math.isnan因为它需要一个浮点参数。在您的countries列表中,您有浮点数和字符串。
In your case the following should suffice:
在您的情况下,以下内容就足够了:
cleanedList = [x for x in countries if str(x) != 'nan']
Old answer
旧答案
In your countrieslist, the literal 'nan'is a string not the Python float nanwhich is equivalent to:
在您的countries列表中,文字'nan'是一个字符串而不是 Python 浮点数nan,它相当于:
float('NaN')
In your case the following should suffice:
在您的情况下,以下内容就足够了:
cleanedList = [x for x in countries if x != 'nan']
回答by Serial
In your example 'nan'is a string so instead of using isnan()just check for the string
在您的示例中'nan'是一个字符串,而不是isnan()仅使用检查字符串
like this:
像这样:
cleanedList = [x for x in countries if x != 'nan']
回答by zhangxaochen
use numpy fancy indexing:
使用 numpy花式索引:
In [29]: countries=np.asarray(countries)
In [30]: countries[countries!='nan']
Out[30]:
array(['USA', 'UK', 'France'],
dtype='|S6')
回答by sparrow
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:
例如,我注意到 Pandas 会为空白值返回 'nan'。由于它不是字符串,因此您需要将其转换为字符串以匹配它。例如:
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)
回答by Yohan Obadia
The problem comes from the fact that np.isnan()does not handle string values correctly. For example, if you do:
问题来自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''
However the pandas version pd.isnull()works for numeric and string values:
然而,pandas 版本pd.isnull()适用于数字和字符串值:
pd.isnull("A")
> False
pd.isnull(3)
> False
pd.isnull(np.nan)
> True
pd.isnull(None)
> True
回答by Ajay Shah
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).
这应该删除所有 NaN。当然,我假设这里不是字符串而是实际的 NaN ( np.nan)。
回答by vlmercado
Using your example where...
使用您的示例,其中...
countries= [nan, 'USA', 'UK', 'France']
countries= [nan, 'USA', 'UK', 'France']
Since nan is not equal to nan (nan != nan) and countries[0] = nan, you should observe the following:
由于 nan 不等于 nan (nan != nan) 并且 countries[0] = nan,您应该注意以下几点:
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]
回答by Beyran11
if you check for the element type
如果您检查元素类型
type(countries[1])
the result will be <class float>so you can use the following code:
结果将是<class float>这样您就可以使用以下代码:
[i for i in countries if type(i) is not float]
回答by Aaron England
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)]
回答by Sorin Dragan
Another way to do it would include using filterlike this:
另一种方法是使用这样的过滤器:
countries = list(filter(lambda x: str(x) != 'nan', countries))

