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'}
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 .
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.
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.
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.
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.
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'}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With