Replacing missing data with column Mean in Pandas
12. Dropping Rows with Missing Data
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.
For more Practice: Solve these Related Problems:
- Write a Pandas program to drop rows with any missing values and report the number of rows removed.
- Write a Pandas program to drop rows with missing values only in a specified subset of columns.
- Write a Pandas program to drop rows with missing values and then reset the DataFrame index.
- Write a Pandas program to drop rows with missing values and compare the shape of the DataFrame before and after dropping.
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.