Nan in if condition python

for r in range[65000]:
    for c in range[8]:
        if df1.iloc[r,c] != NaN:
            k=k+1
            df.iloc[k,3] = df1.iloc[r,c]
        else:
            print["Nan Detected"]
            l=l+1
print[l," Nan Values encountered"]

DeepSpace

74.5k11 gold badges99 silver badges142 bronze badges

asked Jul 12, 2017 at 14:52

5

Unfortunately NaN will compare false, even with itself. So df1.iloc[r,c] != NaN is always true.

Use numpy.isnan[number] or math.isnan[number] instead to check if number is NaN.

answered Jul 12, 2017 at 14:54

BathshebaBathsheba

229k33 gold badges352 silver badges472 bronze badges

1

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.

import numpy as np
x = float["nan"]
print[f"It's np.isnan : {np.isnan[x]}"]
OutputIt's np.isnan : True

Method 3: Using math library

Math library provides has built-in mathematical functions. The library is applicable to all real numbers. cmath library can be used if dealing with complex numbers.
Math library has built in function isnan[] to check null/NaN values.

import math
x = float["nan"]
print[f"It's math.isnan : {math.isnan[x]}"]
OutputIt's math.isnan : True

Method 4: Comparing with itself

When I started my career working with big IT company, I had to undergo a training for the first month. The trainer, when introducing the concept of NaN values mentioned that they are like aliens we know nothing about. These aliens are constantly shapeshifting, and hence we cannot compare NaN value against itself.
The most common method to check for NaN values is to check if the variable is equal to itself. If it is not, then it must be NaN value.

def isNaN[num]:
return num!= num
x=float["nan"]
isNaN[x]
OutputTrue

Method 5: Checking the range

Another property of NaN which can be used to check for NaN is the range. All floating point values fall within the range of minus infinity to infinity.

infinity < any number< infinity

However, NaN values does not come within this range. Hence, NaN can be identified if the value does not fall within the range from minus infinity to infinity.

This can be implemented as below:

def isNaN[num]:
if float['-inf'] < float[num] < float['inf']:
return False
else:
return True
x=float["nan"]
isNaN[x]
OutputTrue

I hope you have found the above article helpful. I am sure there would be many other techniques to check for NaN values based on various other logics. Please share the other methods you have come across to check for NaN/ Null values.

Cheers!

Become a Member

I hope you like the article, I would highly recommend signing up for Medium Membership to read more articles by me or stories by thousands of other authors on variety of topics.
Your membership fee directly supports me and other writers you read. You’ll also get full access to every story on Medium.

How do you check if a value is in NaN in if condition Python?

The math. isnan[] method checks whether a value is NaN [Not a Number], or not. This method returns True if the specified value is a NaN, otherwise it returns False.

How does Python handle NaN?

'nan' in Python.
n1 = float["nan"] n2 = float["Nan"] n3 = float["NaN"] n4 = float["NAN"] print[n1, n2, n3, n4] ... .
import math n1 = math. ... .
print[n1 == n2] print[n1 == 0] print[n1 == 100] print[n2 < 0] ... .
import pandas as pd df = pd. ... .
average_temp_series = df. ... .
sensors = df. ... .
# best practice: df = df..

What is float [' NaN ']?

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.

How do I know if I have NaN?

To check whether the given number is NaN or finite, we can use JavaScript methods. 1. isNaN[] Method: To determine whether a number is NaN, we can use the isNaN[] function. It is a boolean function that returns true if a number is NaN otherwise returns false.

Chủ Đề