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 :
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.
Go to:
Previous: Reordering and Splitting Columns.
Next: Changing Column Names to Lowercase.
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.