Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Median of a list with NaN values removed, in python

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

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

like image 437
wolfsatthedoor Avatar asked Oct 20 '14 21:10

wolfsatthedoor


People also ask

How do I remove a NaN from a list in Python?

To remove nan values from list in python using the math. isnan() function, we will first create an empty list named newList . After that, we will traverse each element of the list using a for loop and check if it is a nan value or not using the math. isnan() function.

How do you exclude NaN values from a list?

To remove NaN from a list using Python, the easiest way is to use the isnan() function from the Python math module and list comprehension. You can also use the Python filter() function. The Python numpy module also provides an isnan() function that we can use to check if a value is NaN.

How do you skip NaN values in Python?

dropna() is used to drop rows with NaN / None values from DataFrame. numpy. nan is Not a Number (NaN), which is of Python build-in numeric type float (floating point).

How do you fix NaN errors in Python?

We can replace NaN values with 0 to get rid of NaN values. This is done by using fillna() function. This function will check the NaN values in the dataframe columns and fill the given value.


1 Answers

numpy 1.9.0 has the function 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.

>>> 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.)

like image 148
Warren Weckesser Avatar answered Nov 15 '22 03:11

Warren Weckesser