How to Check for NaN in Pandas DataFrame?
NaN stands for Not A Number and is one of the popular ways to represent the missing value in the data. NaN value very essential to deal with and is one of the major problems in Data Analysis.
Below are the ways to check for NaN in Pandas DataFrame:
- Check for NaNin a single DataFrame column:
- Count the NaNin a single DataFrame column:
- Check for NaN under the whole DataFrame:
- Count the NaN under the whole DataFrame:
Method 1: Using isnull().values.any() method
Example:
import pandas as pd import numpy as np num = {'Integers': [20, 45, 30, 50, 55, np.nan, 75, np.nan, 100, 150, np.nan]} # Create the dataframe df = pd.DataFrame(num, columns=['Integers']) # Applying the method check_nan = df['Integers'].isnull().values.any() # printing the result print(check_nan)
Output:
True
To get the exact positions where NaN values are present we can use by removing .values.any() from isnull().values.any() .
Example:
check_nan = df['Integers'].isnull() check_nan
Output:
Method 2: Using isnull().sum() Method
Example:
import pandas as pd import numpy as np num = {'Integers': [20, 45, 30, 50, 55, np.nan, 75, np.nan, 100, 150, np.nan]} # Create the dataframe df = pd.DataFrame(num, columns=['Integers']) # applying the method count_nan = df['Integers'].isnull().sum() # printing the number of values present # in the column print('Number of NaN values present: ' + str(count_nan))
Output:
Number of NaN values present: 3
Method 3: Using isnull().values.any() Method to Check for NaN under the whole DataFrame
Example:
import pandas as pd import numpy as np nums = {'Integers_1': [20, 45, 30, 60, 55, np.nan, 75, np.nan, 100, 150, np.nan], 'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25, np.nan, 26, np.nan, np.nan]} # Create the dataframe df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2']) # applying the method nan_in_df = df.isnull().values.any() # Print the dataframe print(nan_in_df)
Output:
True
To get the exact positions where NaN values are present, just remove .values.any() from isnull().values.any() .
Method 4: Using isnull().sum().sum() Method
Example:
import pandas as pd import numpy as np nums = {'Integers_1': [20, 45, 30, 60, 55, np.nan, 75, np.nan, 100, 150, np.nan], 'Integers_2': [np.nan, 21, 22, 23, np.nan, 24, 25, np.nan, 26, np.nan, np.nan]} # Create the dataframe df = pd.DataFrame(nums, columns=['Integers_1', 'Integers_2']) # applying the method nan_in_df = df.isnull().sum().sum() # printing the number of values present in # the whole dataframe print('Number of NaN values present: ' + str(nan_in_df))
Output:
Number of NaN values present: 8
Share: