Member-only story
5 Methods to Check for NaN values in in Python
How to check if a single value is NaN in python. There are approaches are using libraries (pandas, math and numpy) and without using libraries.
NaN stands for Not A Number and is one of the common ways to represent the missing value in the data. It is a special floating-point value and cannot be converted to any other type than float.
NaN value is one of the major problems in Data Analysis. It is very essential to deal with NaN in order to get the desired results.
Finding and dealing with NaN within an array, series or dataframe is easy. However, identifying a stand alone NaN value is tricky. In this article I explain five methods to deal with NaN in python. The first three methods involves in-built functions from libraries. The last two relies on properties of NaN for finding NaN values.
Method 1: Using Pandas Library
isna() in pandas library can be used to check if the value is null/NaN. It will return True if the value is NaN/null.
import pandas as pd
x = float("nan")
print(f"It's pd.isna : {pd.isna(x)}")OutputIt's pd.isna : True
Method 2: Using Numpy Library
isnan() in numpy library can be used to check if the value is null/NaN. It is similar to isna() in pandas.