pandas 在 python 中删除 NaN 值的列表的中位数

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

Median of a list with NaN values removed, in python

pythonnumpypandasmedian

提问by wolfsatthedoor

Is it possible to calculate the median of a list without explicitly removing the NaN's, but rather, ignoring them?

是否可以在不明确删除 NaN 的情况下计算列表的中位数,而是忽略它们?

I want median([1,2,3,NaN,NaN,NaN,NaN,NaN,NaN])to be 2, not NaN.

我想median([1,2,3,NaN,NaN,NaN,NaN,NaN,NaN])成为 2,而不是 NaN。

回答by Warren Weckesser

numpy 1.9.0 has the function nanmedian:

numpy 1.9.0 具有以下功能nanmedian

nanmedian(a, axis=None, out=None, overwrite_input=False, keepdims=False)
    Compute the median along the specified axis, while ignoring NaNs.

    Returns the median of the array elements.

    .. versionadded:: 1.9.0

E.g.

例如

>>> from numpy import nanmedian, NaN
>>> nanmedian([1,2,3,NaN,NaN,NaN,NaN,NaN,NaN])
2.0

If you can't use version 1.9.0 of numpy, something like @Parker's answer will work; e.g.

如果您不能使用 numpy 的 1.9.0 版,则可以使用@Parker 的回答之类的方法;例如

>>> import numpy as np
>>> x = np.array([1,2,3,NaN,NaN,NaN,NaN,NaN,NaN])
>>> np.median(x[~np.isnan(x)])
2.0

or

或者

>>> np.median(x[np.isfinite(x)])
2.0

(When applied to a boolean array, ~is the unary operator notation for not.)

(当应用于布尔数组时,~是 的一元运算符符号not。)

回答by Parker

I would clean the list of all NaN's, and then get the median of the cleaned list. There're two ways that come to mind. If you're using the numpy library, you can do:

我会清理所有 NaN 的列表,然后获取清理列表的中位数。我想到了两种方法。如果您使用的是 numpy 库,则可以执行以下操作:

x = x[numpy.logical_not(numpy.isnan(x))]where xis the list you want to get the median of

x = x[numpy.logical_not(numpy.isnan(x))]x您想要获得中位数的列表 在哪里

Or, if you just want to use the included libraries you can do:

或者,如果您只想使用包含的库,您可以执行以下操作:

import math
x = [value for value in x if not math.isnan(value)]

Then to get the median just use the cleaned list: `median(x)``

然后要获得中位数,只需使用清理过的列表:`median(x)``