Handling missing data is one of the most important tasks in data analysis. Real-world datasets often contain missing values due to incomplete records, data entry errors, or system failures. Pandas provides several functions such as isnull(), notnull(), dropna(), and fillna() to identify, remove, and replace missing values efficiently. In this chapter, you’ll learn how to handle missing data using practical examples. Pandas Missing Data Handling practice questions with solutions help to understand the concepts.
1. Python Program to Check Missing Values Using isnull()
Problem Statement
Write a Python program to identify missing values in a DataFrame.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
print(df.isnull())
Sample Output
Name Marks
0 False False
1 False True
2 False False
Explanation
The isnull() function returns True for missing values and False for non-missing values.
Concepts Covered
isnull()- Missing Values
- Boolean Output
2. Python Program to Check Non-Missing Values Using notnull()
Problem Statement
Write a Python program to identify non-missing values in a DataFrame.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
print(df.notnull())
Sample Output
Name Marks
0 True True
1 True False
2 True True
Explanation
The notnull() function returns True for available values and False for missing values.
Concepts Covered
notnull()- Missing Data Detection
- Boolean Mask
3. Python Program to Count Missing Values
Problem Statement
Write a Python program to count the total missing values in each column.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", None, "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
print(df.isnull().sum())
Sample Output
Name 1
Marks 1
dtype: int64
Explanation
The sum() function counts the number of True values returned by isnull().
Concepts Covered
isnull()sum()- Missing Value Count
4. Python Program to Remove Rows Containing Missing Values
Problem Statement
Write a Python program to remove rows containing missing values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
result = df.dropna()
print(result)
Sample Output
Name Marks
0 Rahul 85.0
2 Priya 90.0
Explanation
The dropna() function removes rows that contain one or more missing values.
Concepts Covered
dropna()- Remove Missing Data
- Data Cleaning
5. Python Program to Fill Missing Values with a Constant
Problem Statement
Write a Python program to replace missing values with 0.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
result = df.fillna(0)
print(result)
Sample Output
Name Marks
0 Rahul 85.0
1 Aman 0.0
2 Priya 90.0
Explanation
The fillna() function replaces missing values with the specified constant.
Concepts Covered
fillna()- Missing Value Replacement
- Data Cleaning
6. Python Program to Fill Missing Values with the Column Mean
Problem Statement
Write a Python program to replace missing values in the Marks column with the average marks.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 95]
}
df = pd.DataFrame(data)
df["Marks"] = df["Marks"].fillna(
df["Marks"].mean()
)
print(df)
Sample Output
Name Marks
0 Rahul 85.0
1 Aman 90.0
2 Priya 95.0
Explanation
The mean() function calculates the average of available values, and fillna() replaces missing values with that average.
Concepts Covered
fillna()mean()- Average Imputation
7. Python Program to Fill Missing Values with the Column Median
Problem Statement
Write a Python program to replace missing values with the median value.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [80, np.nan, 90, 100]
}
df = pd.DataFrame(data)
df["Marks"] = df["Marks"].fillna(
df["Marks"].median()
)
print(df)
Sample Output
Marks
0 80.0
1 90.0
2 90.0
3 100.0
Explanation
The median() function returns the middle value, making it useful when the data contains outliers.
Concepts Covered
fillna()median()- Missing Value Imputation
8. Python Program to Fill Missing Values with the Most Frequent Value
Problem Statement
Write a Python program to replace missing values with the most frequently occurring value.
Python Solution
import pandas as pd
import numpy as np
data = {
"City": [
"Delhi",
np.nan,
"Delhi",
"Mumbai"
]
}
df = pd.DataFrame(data)
df["City"] = df["City"].fillna(
df["City"].mode()[0]
)
print(df)
Sample Output
City
0 Delhi
1 Delhi
2 Delhi
3 Mumbai
Explanation
The mode() function returns the most frequently occurring value in the column.
Concepts Covered
fillna()mode()- Categorical Data
9. Python Program to Remove Columns Containing Missing Values
Problem Statement
Write a Python program to remove columns containing missing values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman"],
"Marks": [85, np.nan],
"City": ["Delhi", "Mumbai"]
}
df = pd.DataFrame(data)
result = df.dropna(axis=1)
print(result)
Sample Output
Name City
0 Rahul Delhi
1 Aman Mumbai
Explanation
Using axis=1 tells Pandas to remove columns that contain missing values.
Concepts Covered
dropna()axis=1- Remove Columns
10. Python Program to Fill Missing Values Using Forward Fill
Problem Statement
Write a Python program to fill missing values using the previous available value.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [80, np.nan, np.nan, 95]
}
df = pd.DataFrame(data)
result = df.ffill()
print(result)
Sample Output
Marks
0 80.0
1 80.0
2 80.0
3 95.0
Explanation
The ffill() (forward fill) method replaces missing values with the last valid value found above them.
Concepts Covered
ffill()- Forward Fill
- Missing Data Handling
11. Python Program to Fill Missing Values Using Backward Fill
Problem Statement
Write a Python program to replace missing values using the next available value.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [80, np.nan, np.nan, 95]
}
df = pd.DataFrame(data)
result = df.bfill()
print(result)
Sample Output
Marks
0 80.0
1 95.0
2 95.0
3 95.0
Explanation
The bfill() (backward fill) method replaces missing values using the next valid value below them.
Concepts Covered
bfill()- Backward Fill
- Missing Data Handling
12. Python Program to Replace Missing Values in a Specific Column
Problem Statement
Write a Python program to replace missing values only in the Salary column.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Salary": [50000, np.nan, 60000]
}
df = pd.DataFrame(data)
df["Salary"] = df["Salary"].fillna(55000)
print(df)
Sample Output
Name Salary
0 Rahul 50000.0
1 Aman 55000.0
2 Priya 60000.0
Explanation
Selecting a specific column before using fillna() updates only that column.
Concepts Covered
fillna()- Column Selection
- Data Cleaning
13. Python Program to Drop Rows Only When All Values Are Missing
Problem Statement
Write a Python program to remove rows where every column contains missing values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", np.nan, "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
result = df.dropna(how="all")
print(result)
Sample Output
Name Marks
0 Rahul 85.0
2 Priya 90.0
Explanation
The how="all" parameter removes only those rows where every value is missing.
Concepts Covered
dropna()how="all"- Row Filtering
14. Python Program to Drop Rows Having Missing Values in a Specific Column
Problem Statement
Write a Python program to remove rows where the Marks column contains missing values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya"],
"Marks": [85, np.nan, 90]
}
df = pd.DataFrame(data)
result = df.dropna(
subset=["Marks"]
)
print(result)
Sample Output
Name Marks
0 Rahul 85.0
2 Priya 90.0
Explanation
The subset parameter checks only the specified column before removing rows.
Concepts Covered
dropna()subset- Selective Row Removal
15. Python Program to Replace Missing Values in Multiple Columns
Problem Statement
Write a Python program to replace missing values in multiple columns using different values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [85, np.nan, 90],
"City": ["Delhi", np.nan, "Mumbai"]
}
df = pd.DataFrame(data)
result = df.fillna(
{
"Marks": 0,
"City": "Unknown"
}
)
print(result)
Sample Output
Marks City
0 85.0 Delhi
1 0.0 Unknown
2 90.0 Mumbai
Explanation
A dictionary can be passed to fillna() to specify different replacement values for different columns.
Concepts Covered
fillna()- Dictionary Mapping
- Multiple Column Replacement
16. Python Program to Calculate the Total Missing Values in a DataFrame
Problem Statement
Write a Python program to calculate the total number of missing values in an entire DataFrame.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", None, "Priya"],
"Marks": [85, np.nan, 90],
"City": ["Delhi", None, "Mumbai"]
}
df = pd.DataFrame(data)
total_missing = df.isnull().sum().sum()
print("Total Missing Values:", total_missing)
Sample Output
Total Missing Values: 3
Explanation
The first sum() counts missing values column-wise, while the second sum() adds those counts to get the total missing values in the DataFrame.
Concepts Covered
isnull()sum()- Missing Value Count
17. Python Program to Replace Missing Values with Different Statistical Methods
Problem Statement
Write a Python program to replace missing values using the column mean and mode.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [80, np.nan, 90, 100],
"City": ["Delhi", np.nan, "Delhi", "Mumbai"]
}
df = pd.DataFrame(data)
df["Marks"] = df["Marks"].fillna(
df["Marks"].mean()
)
df["City"] = df["City"].fillna(
df["City"].mode()[0]
)
print(df)
Sample Output
Marks City
0 80.0 Delhi
1 90.0 Delhi
2 90.0 Delhi
3 100.0 Mumbai
Explanation
Numeric columns are commonly filled using statistical values such as the mean, while categorical columns are usually filled using the mode. This approach helps preserve the overall structure of the dataset.
Concepts Covered
fillna()mean()mode()- Missing Data Imputation
Chapter Summary
In this chapter, you learned how to identify, count, remove, and replace missing values using Pandas. You practiced using isnull(), notnull(), dropna(), fillna(), ffill(), and bfill(). You also learned how to replace missing values using statistical methods like mean, median, and mode, remove rows or columns selectively, and calculate the total number of missing values. These techniques are essential for data cleaning and preparing datasets for analysis and machine learning.
Key Takeaways
isnull()identifies missing values.notnull()identifies available values.dropna()removes rows or columns containing missing values.fillna()replaces missing values.ffill()fills missing values using the previous valid value.bfill()fills missing values using the next valid value.- Mean, median, and mode are commonly used for data imputation.
- The
subsetparameter removes rows based on selected columns. - Dictionaries can assign different replacement values to different columns.
- Proper missing data handling improves data quality and analysis accuracy.
Frequently Asked Questions (FAQs)
1. How do you identify missing values in Pandas?
Use the isnull() function.
df.isnull()
2. How do you count missing values in each column?
df.isnull().sum()
3. How do you remove rows containing missing values?
df.dropna()
4. How do you replace missing values with zero?
df.fillna(0)
5. What is the difference between ffill() and bfill()?
ffill()copies the previous valid value.bfill()copies the next valid value.
6. Which statistical methods are commonly used to fill missing values?
The most commonly used methods are:
- Mean
- Median
- Mode
7. How do you remove rows based on missing values in a specific column?
df.dropna(subset=["Marks"])
8. Why is handling missing data important in Pandas?
Missing data can lead to incorrect analysis, poor visualizations, and inaccurate machine learning models. Cleaning missing values ensures better data quality, reliable insights, and improved model performance.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
