Python 从列表中删除 nan

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45695373/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:13:09  来源:igfitidea点击:

Removing a nan from a list

pythonnan

提问by ???? ?????

While trying to work on a project with pandas I have run into a problem. I had a list with a nanvalue in it and I couldn't remove it.

在尝试使用 Pandas 进行项目时,我遇到了一个问题。我有一个包含nan值的列表,但无法删除它。

I have tried:

我试过了:

incoms=data['int_income'].unique().tolist()
incoms.remove('nan')

But it didn't work:

但它没有用:

list.remove(x): x not in list"

list.remove(x): x 不在列表中”

The list incomsis as follows:

名单incoms如下:

[75000.0, 50000.0, 0.0, 200000.0, 100000.0, 25000.0, nan, 10000.0, 175000.0, 150000.0, 125000.0]

回答by jezrael

I think you need dropnafor remove NaNs:

我认为您需要dropna删除NaNs:

incoms=data['int_income'].dropna().unique().tolist()
print (incoms)
[75000.0, 50000.0, 0.0, 200000.0, 100000.0, 25000.0, 10000.0, 175000.0, 150000.0, 125000.0]

And if all values are integers only:

如果所有值都只是整数:

incoms=data['int_income'].dropna().astype(int).unique().tolist()
print (incoms)
[75000, 50000, 0, 200000, 100000, 25000, 10000, 175000, 150000, 125000]

Or remove NaNs by selecting all non NaN values by numpy.isnan:

或者NaN通过以下方式选择所有非 NaN 值来删除s numpy.isnan

a = data['int_income'].unique()
incoms= a[~np.isnan(a)].tolist()
print (incoms)
[75000.0, 50000.0, 0.0, 200000.0, 100000.0, 25000.0, 10000.0, 175000.0, 150000.0, 125000.0]


a = data['int_income'].unique()
incoms= a[~np.isnan(a)].astype(int).tolist()
print (incoms)
[75000, 50000, 0, 200000, 100000, 25000, 10000, 175000, 150000, 125000]

Pure python solution - slowier if big DataFrame:

纯 python 解决方案 - 如果大则更慢DataFrame

incoms=[x for x in  list(set(data['int_income'])) if pd.notnull(x)]
print (incoms)
[0.0, 100000.0, 200000.0, 25000.0, 125000.0, 50000.0, 10000.0, 150000.0, 175000.0, 75000.0]


incoms=[int(x) for x in  list(set(data['int_income'])) if pd.notnull(x)]
print (incoms)
[0, 100000, 200000, 25000, 125000, 50000, 10000, 150000, 175000, 75000]

回答by zoubida13

What you can do is simply get a cleaned list where you don't put the values that, once converted to strings, are 'nan'.

您可以做的只是获得一个清理过的列表,您不要在其中放置一旦转换为字符串的值为“nan”的值。

The code would be :

代码将是:

incoms = [incom for incom in incoms if str(incom) != 'nan']

回答by Rafael Valero

A possibility in that particular case is to remove nans earlier to avoid to do it in the list:

在这种特殊情况下的一种可能性是提前删除 nans 以避免在列表中执行它:

incoms=data['int_income'].dropna().unique().tolist()