Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove NaN value from a set

Tags:

python

Is it possible to easily remove NaN values for a Python Set object? Given that NaN values do not equal anything (and float('nan') is float('nan') is also False), you can end up with many NaN values in a Set.

>>> a = set( (float('nan'), float('nan'), 'a') )
>>> a
{nan, nan, 'a'}

The best I can come up with it to define a function like math.isnan, but that is tolerant of non-float types like:

def my_isnan(x):
    try:
        return math.isnan(x)
    except TypeError:
        return False

Then you can use a set comprehension like this:

>>> {x for x in a if not my_isnan(x)}
{'a'}
like image 550
Brad Campbell Avatar asked May 10 '16 19:05

Brad Campbell


People also ask

How do I remove NaN values from a data set?

By using dropna() method you can drop rows with NaN (Not a Number) and None values from pandas DataFrame. Note that by default it returns the copy of the DataFrame after removing rows. If you wanted to remove from the existing DataFrame, you should use inplace=True .

How do I get rid of NaN error?

Nan means “Not a number”, this is because inside your cube function, you're not calling the square function, but getting it's contents. Change return x * square; with return x * square(x); and it should work.

How do I remove a NaN value in R studio?

The NaN values are referred to as the Not A Number in R. It is also called undefined or unrepresentable but it belongs to numeric data type for the values that are not numeric, especially in case of floating-point arithmetic. To remove rows from data frame in R that contains NaN, we can use the function na. omit.

How do you find the NaN value of a list?

The math. isnan(value) method takes a number value as input and returns True if the value is a NaN value and returns False otherwise. Therefore we can check if there a NaN value in a list or array of numbers using the math. isnan() method.


2 Answers

In practice, you could look at the fact that nan != nan as a feature, not a bug:

>>> a = {float('nan'), float('nan'), 'a'}
>>> a
{nan, nan, 'a'}
>>> {x for x in a if x==x}
{'a'}

On the positive side, no need for a helper function. On the negative side, if you have a non-nan object which is also not equal to itself, you'll remove that too.

like image 125
DSM Avatar answered Oct 13 '22 23:10

DSM


Use pd.notna() from pandas, e.g.:

In [219]: import pandas as pd

In [220]: a = set((float('nan'), float('nan'), 'a'))

In [221]: a = {x for x in a if pd.notna(x)}

In [222]: a
Out[222]: {'a'}
like image 35
Pawel Kranzberg Avatar answered Oct 14 '22 00:10

Pawel Kranzberg