w3resource

Replacing missing data with column Mean in Pandas


Pandas: Data Cleaning and Preprocessing Exercise-12 with Solution


Write a Pandas program to replacing missing data with mean value.

This exercise shows how to replace missing values in a numerical column with the mean of that column.

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with missing values
df = pd.DataFrame({
    'Name': ['Selena', 'Annabel', 'Caeso', 'David'],
    'Age': [25, None, 22, None]
})

# Fill missing 'Age' values with the column's mean
df['Age'].fillna(df['Age'].mean(), inplace=True)

# Output the result
print(df)

Output:

      Name   Age
0   Selena  25.0
1  Annabel  23.5
2    Caeso  22.0
3    David  23.5

Explanation:

  • Created a DataFrame with missing values in the 'Age' column.
  • Replaced missing values in 'Age' with the mean of the column using fillna().
  • Returned the DataFrame with missing data replaced by the mean value.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.