# By comparing itself
def is_nan(val):
return val != val
nan_value = float("nan")
print(is_nan(nan_value))
# -> True
#Using math.isnan(Import math before using math.isnan)
import math
nan_value = float("NaN")
check_nan = math.isnan(nan_value)
print(check_nan)
# -> True
In the above code examples, we are not using any python library and created a function is_nan() that will take the value as a parameter and will compare the value with itself. If the value is NaN then it will return true and if the value is not NaN it will return false.
In the Python programming language, NaN is considered as a float value so we converted NaN first to the float function of python then passed it to the function that will check if the value is NaN or not.
import math
my_value = float("NaN")
is_nan_val = math.isnan(my_value)
print(is_nan_val)
# -> True
import numpy as np
value = float("nan")
check_nan = np.isnan(value)
import pandas as pd
value = float("nan")
check_isnan = pd.isna(value)
print(check_isnan)
# -> True
def check_nan(x):
return str(float(x)).lower() == 'nan'
print(check_nan('nan'))
# -> True
0 Comments