Count the number of missing values in each column of a DataFrame

In this python pandas program, we will count the number of missing values in each column of a DataFrame.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Count the number of missing values in each column of a DataFrame using df.isna().sum().
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex',np.nan,'Peter','Klaus'],
                   'Age':[30,np.nan,29,np.nan]})
print("Original Dataframe: \n",df)
print("Identify the columns which have at least one missing value:")
print(df.isna().sum())
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2    NaN   NaN
2       3  Peter  29.0
3       4  Klaus   NaN
Identify the columns which have at least one missing value:
Sr.no.    0
Name      1
Age       2
dtype: int64
				
			

identify the columns from the DataFrame which have at least one missing value

drop the rows where at least one element is missing in a DataFrame

Leave a Comment