The table below shows if the type of object can be checked with the given method:
+------------+-----+---------+------+--------+------+
| Method | NaN | numeric | None | string | list |
+------------+-----+---------+------+--------+------+
| pd.isna | yes | yes | yes | yes | yes |
| math.isnan | yes | yes | no | no | no |
| np.isnan | yes | yes | no | no | yes | <-- # will error on mixed type list
+------------+-----+---------+------+--------+------+
pd.isna
The most flexible method to check for different types of missing values.
None of the answers cover the flexibility of pd.isna. While
math.isnan and np.isnan will return True for NaN values, you cannot check for different type of objects like None or strings. Both methods will return an error, so checking a list with mixed types will be cumbersom. This while pd.isna is flexible and will return the correct boolean for different kind of types:
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: missing_values = [3, None, np.NaN, pd.NA, pd.NaT, '10']
In [4]: pd.isna(missing_values)
Out[4]: array([False, True, True, True, True, False])
Nội dung chính
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.
Method 1: Using Pandas Library
Method 2: Using Numpy Library
Method 3: Using math library
Method 4: Comparing with itself
Method 5: Checking the range
Become a Member
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.
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.
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.
Bạn có một cặp đôi tùy chọn.
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10,6))# Make a few areas have NaN values
df.iloc[1:3,1]= np.nan
df.iloc[5,3]= np.nan
df.iloc[7:9,5]= np.nan
Nếu bạn tạo nó df.isnull().any(), bạn chỉ có thể tìm thấy các cột có NaNgiá trị:
0False1True2False3True4False5True
dtype: bool
Một người nữa .any()sẽ cho bạn biết nếu có bất kỳ điều nào ở trênTrue
> df.isnull().any().any()True
Tùy chọn 2 : df.isnull().sum().sum()- Điều này trả về một số nguyên của tổng số NaNgiá trị:
Điều này hoạt động theo cách tương tự như trước .any().any(), bằng cách trước tiên đưa ra tổng của số
lượng NaNgiá trị trong một cột, sau đó là tổng của các giá trị đó:
df.isnull().sum()001220314052
dtype: int64
Cuối cùng, để có được tổng số giá trị NaN trong DataFrame: