Python Pandas program to replace NaNs with the median value in a DataFrame

In this python pandas program, we will replace NaNs with the median value using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Create a dataframe using pd.DataFrame().
  3. Now replace the NaN values in the age column with the median value of the same column using df[‘Age’].fillna(df[‘Age’].median(),inplace=True).
  4. Print the output.
				
					import pandas as pd
import numpy as np
df = pd.DataFrame({'Sr.no.':[1,2,3,4],
                   'Name':['Alex','John','Peter','Klaus'],
                   'Age':[30,np.nan,29,22]})
print("Original Dataframe: \n",df)
df['Age'].fillna(df['Age'].median(),inplace=True)
print("After replacing missing values by mean: \n",df)
				
			

Output :

				
					Original Dataframe: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John   NaN
2       3  Peter  29.0
3       4  Klaus  22.0
After replacing missing values by mean: 
    Sr.no.   Name   Age
0       1   Alex  30.0
1       2   John  29.0
2       3  Peter  29.0
3       4  Klaus  22.0
				
			

replace the missing values with the most frequent values present in each column of a given DataFrame

import a CSV file

1 thought on “Python Pandas program to replace NaNs with the median value in a DataFrame”

Leave a Comment