Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to check if any value is NaN in a Pandas DataFrame?
To check if any value is NaN in a Pandas DataFrame, we can use isnull().values.any() method.
Steps
Make a series, s, one-dimensional ndarray with axis labels (including time series).
Print the series, s.
Check whether NaN is present or not.
Create a two-dimensional, size-mutable, potentially heterogeneous tabular data, df.
Print the input DataFrame.
Check whether NaN is present or not.
Example
import pandas as pd
import numpy as np
s = pd.Series([1, np.nan, 3, np.nan, 3, np.nan, 7, np.nan, 3])
print "Input series is:<br>", s
present = s.isnull().values.any()
print "NAN is present in series: ", present
df = pd.DataFrame(
{
"x": [5, np.nan, 1, np.nan],
"y": [np.nan, 1, np.nan, 10],
"z": [np.nan, 1, np.nan, np.nan]
}
)
print "\nInput DataFrame is:<br>", df
present = df.isnull().values.any()
print "\nNAN present in DataFrame:", present
Output
Input series is: 0 1.0 1 NaN 2 3.0 3 NaN 4 3.0 5 NaN 6 7.0 7 NaN 8 3.0 dtype: float64 NAN is present in series: True Input DataFrame is: x y z 0 5.0 NaN NaN 1 NaN 1.0 1.0 2 1.0 NaN NaN 3 NaN 10.0 NaN NAN present in DataFrame: True
Advertisements
