In this python pandas program, we will count the NaN values in a Dataframe using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Import NumPy library as np.
- Create a dataframe using pd.DataFrame().
- Count the NaN values in a Dataframe using df.isnull().values.sum().
- Print the output.
import pandas as pd
import numpy as np
d = {'Sr.no.':[1,2,3,4],'Name':['Alex','John','Peter','Klaus'],'Age':[30,np.nan,29,np.nan]}
df = pd.DataFrame(d)
print(df)
print("Nan values in the dataframe: ",df.isnull().values.sum())
Output :
0 Sr.no. Name Age
0 1 Alex 30.0
1 2 John NaN
2 3 Peter 29.0
3 4 Klaus NaN
Nan values in the dataframe: 2