Javatpoint Logo
Javatpoint Logo

How to check nan in Python?

In Python, "NaN" stands for "Not a Number" and is a special value used to represent missing or undefined numeric data. It is a special value defined by the IEEE (Institute of Electrical and Electronics Engineers) standard for floating-point arithmetic. It is often used to represent missing or undefined data in a dataset. Checking for NaN values is an important step in data preprocessing and cleaning, as these values can cause errors or unexpected behavior in downstream calculations.

Python has several ways to check for NaN values, but built-in math is the most common method .isnan() function or using the numpy library.

The math.isnan() function is a built-in Python function that can be used to check if a given value is NaN. It just requires one input, the value to be verified, and if the value is NaN, returns True; otherwise, it returns False.

For example:

Output:

True
False

Using math.isnan() is a simple and effective way to check for NaN values in individual variables, but it can be less efficient when working with large arrays of data.

Another common way to check for NaN values in Python is by using the numpy library. The NumPy library provides a number of functions for working with arrays of data, including an isnan() function that can be used to check for NaN values in an array. The numpy.isnan() function takes an array as an argument and returns an array of the same shape with True at the positions where the input has NaN and False elsewhere.

For example:

Output:

[False  True False  True]

In addition to numpy.isnan(). Other numpy functions are used for checking NaN values like np.isnan(), which returns a boolean mask of the same shape as the input array.

You can also use the pd.isna() function from the panda's library to check for NaN values in a DataFrame or Series. This function returns a boolean mask, with True for cells containing NaN values and False for cells containing valid values.

For example:

You can also use the pd.isnull() function, which is equivalent to pd.isna().

You can also use the pd.DataFrame.fillna() function to replace NaN values with a specified value. The value to be used as a replacement for NaN values is the only input this function accepts.

For example:

This will replace all NaN values in the DataFrame with 0.

Another way to check for NaN values in Python is by using the numpy.isnan() function in combination with the numpy.any() or numpy.all() functions. The numpy.any() function returns True if any element in an array is True and False. On the other hand, the numpy.all() function returns True if every element in an array is True and False otherwise.

For example, to check if any value in an array is NaN, you can use the following code:

Output:

True

It will return True because one of the elements in the array is NaN.

Similarly, to check if all values in an array are NaN, you can use the following code:

For example:

Output:

True

This will return True because all elements in the array are NaN.

You can also use the numpy.isinf() function to check for infinite values in an array. The numpy.isinf() function works in a similar way to the numpy.isnan() function, and returns an array of the same shape with True at the positions where the input has infinite value and False elsewhere.

Another way to check for NaN values in Python is by using pandas.isna() method. This method is a DataFrame and Series method and returns a boolean mask of the same shape as the input, with True for cells containing NaN values, and False for cells containing valid values.

For example, if you have a DataFrame df with columns 'A' and 'B' and you want to check for NaN values in column 'A' you can use:

This will return a boolean mask where the positions of NaN values in column 'A' will be True.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA