Pandas Missing Values Handling Practice Questions with Solutions

Handling missing values is one of the most important tasks in data cleaning. Real-world datasets often contain empty cells, null values, or missing records. Pandas provides functions such as isnull(), notnull(), dropna(), fillna(), and replace() to detect and handle missing data efficiently. These techniques are widely used in data preprocessing, business intelligence, machine learning, and data analytics. Pandas Missing Values Handling Practice questions with solutions help to understand the concepts.


1. Python Program to Detect Missing Values

Problem Statement

Write a Python program to identify missing values in a DataFrame.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Employee": ["Rahul", "Aman", np.nan],
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

print(df.isnull())

Sample Output

   Employee  Salary
0     False   False
1     False    True
2      True   False

Explanation

The isnull() function returns True for missing values and False for available values.

Concepts Covered

  • isnull()
  • Missing Values
  • Data Cleaning

2. Python Program to Count Missing Values

Problem Statement

Write a Python program to count missing values in each column.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Employee": ["Rahul", np.nan, "Priya"],
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

print(df.isnull().sum())

Sample Output

Employee    1
Salary      1
dtype: int64

Explanation

Using sum() after isnull() counts the total missing values in every column.

Concepts Covered

  • isnull()
  • sum()
  • Missing Value Count

3. Python Program to Display Non-Missing Values

Problem Statement

Write a Python program to identify non-missing values.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Marks": [80, np.nan, 95]
}

df = pd.DataFrame(data)

print(df.notnull())

Sample Output

   Marks
0   True
1  False
2   True

Explanation

The notnull() function returns True for valid values and False for missing values.

Concepts Covered

  • notnull()
  • Data Validation
  • Missing Data

4. Python Program to Remove Rows with 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 = {
    "Employee": ["Rahul", "Aman", np.nan],
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

result = df.dropna()

print(result)

Sample Output

  Employee   Salary
0    Rahul  50000.0

Explanation

The dropna() function removes rows containing one or more missing values.

Concepts Covered

  • dropna()
  • Row Removal
  • Data Cleaning

5. Python Program to Fill Missing Values with Zero

Problem Statement

Write a Python program to replace missing values with 0.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

result = df.fillna(0)

print(result)

Sample Output

    Salary
0  50000.0
1      0.0
2  60000.0

Explanation

The fillna() function replaces missing values with the specified value.

Concepts Covered

  • fillna()
  • Replace Missing Values
  • Data Cleaning

6. Python Program to Fill Missing Values with the Column Mean

Problem Statement

Write a Python program to replace missing salary values with the average salary.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Salary": [50000, np.nan, 60000, 55000]
}

df = pd.DataFrame(data)

df["Salary"] = df["Salary"].fillna(
    df["Salary"].mean()
)

print(df)

Sample Output

    Salary
0  50000.0
1  55000.0
2  60000.0
3  55000.0

Explanation

The mean() function calculates the average value, and fillna() replaces missing values with that average.

Concepts Covered

  • fillna()
  • mean()
  • Data Imputation

7. Python Program to Fill Missing Values with the Column Median

Problem Statement

Write a Python program to replace missing values using the median.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Marks": [80, 90, np.nan, 70, 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   85.0
3   70.0
4  100.0

Explanation

The median is often preferred over the mean when the data contains outliers.

Concepts Covered

  • median()
  • fillna()
  • Missing Value Handling

8. Python Program to Fill Missing Values Using Forward Fill

Problem Statement

Write a Python program to replace missing values using the previous available value.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Sales": [100, np.nan, np.nan, 250]
}

df = pd.DataFrame(data)

result = df.ffill()

print(result)

Sample Output

   Sales
0  100.0
1  100.0
2  100.0
3  250.0

Explanation

Forward Fill (ffill) copies the previous valid value into missing cells.

Concepts Covered

  • ffill()
  • Forward Fill
  • Missing Data

9. 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 = {
    "Sales": [100, np.nan, np.nan, 250]
}

df = pd.DataFrame(data)

result = df.bfill()

print(result)

Sample Output

   Sales
0  100.0
1  250.0
2  250.0
3  250.0

Explanation

Backward Fill (bfill) replaces missing values with the next available value.

Concepts Covered

  • bfill()
  • Backward Fill
  • Data Cleaning

10. 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 = {
    "Employee": ["Rahul", "Aman"],
    "Salary": [50000, np.nan],
    "Department": ["IT", "HR"]
}

df = pd.DataFrame(data)

result = df.dropna(axis=1)

print(result)

Sample Output

  Employee Department
0    Rahul         IT
1     Aman         HR

Explanation

Using axis=1 with dropna() removes columns that contain missing values.

Concepts Covered

  • dropna()
  • axis=1
  • Column Removal

11. Python Program to Replace Specific Values with NaN

Problem Statement

Write a Python program to replace all occurrences of 0 with NaN.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Marks": [80, 0, 95, 0, 88]
}

df = pd.DataFrame(data)

result = df.replace(0, np.nan)

print(result)

Sample Output

   Marks
0   80.0
1    NaN
2   95.0
3    NaN
4   88.0

Explanation

The replace() function replaces specific values with another value, including NaN.

Concepts Covered

  • replace()
  • NaN
  • Data Cleaning

12. Python Program to Check if a DataFrame Contains Missing Values

Problem Statement

Write a Python program to determine whether a DataFrame contains any missing values.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

print(df.isnull().values.any())

Sample Output

True

Explanation

The values.any() method returns True if at least one missing value exists in the DataFrame.

Concepts Covered

  • isnull()
  • any()
  • Missing Value Detection

13. Python Program to Remove Rows Where All Values Are Missing

Problem Statement

Write a Python program to remove rows in which every value is missing.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Employee": ["Rahul", np.nan, "Aman"],
    "Salary": [50000, np.nan, 45000]
}

df = pd.DataFrame(data)

result = df.dropna(how="all")

print(result)

Sample Output

  Employee   Salary
0    Rahul  50000.0
2     Aman  45000.0

Explanation

Using how="all" removes only those rows where every column contains missing values.

Concepts Covered

  • dropna()
  • how="all"
  • Data Cleaning

14. Python Program to Remove Duplicate Rows After Handling Missing Values

Problem Statement

Write a Python program to fill missing values and then remove duplicate rows.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Employee": ["Rahul", "Rahul", np.nan],
    "Salary": [50000, 50000, 50000]
}

df = pd.DataFrame(data)

df = df.fillna("Unknown")

result = df.drop_duplicates()

print(result)

Sample Output

  Employee  Salary
0    Rahul   50000
2  Unknown   50000

Explanation

After replacing missing values, the drop_duplicates() function removes duplicate records.

Concepts Covered

  • fillna()
  • drop_duplicates()
  • Data Cleaning

15. Python Program to Fill Different Missing Values for Different Columns

Problem Statement

Write a Python program to fill missing values in different columns using different replacement values.

Python Solution

import pandas as pd
import numpy as np

data = {
    "Employee": ["Rahul", np.nan, "Priya"],
    "Salary": [50000, np.nan, 60000]
}

df = pd.DataFrame(data)

result = df.fillna({
    "Employee": "Unknown",
    "Salary": 0
})

print(result)

Sample Output

  Employee   Salary
0    Rahul  50000.0
1  Unknown      0.0
2    Priya  60000.0

Explanation

A dictionary can be passed to fillna() to specify different replacement values for different columns.

Concepts Covered

  • fillna()
  • Dictionary Replacement
  • Missing Value Handling

Chapter Summary

In this chapter, you learned how to detect, count, replace, and remove missing values in Pandas. You practiced using isnull(), notnull(), dropna(), fillna(), replace(), ffill(), and bfill(). You also learned how to replace missing values with the mean, median, custom values, and different values for different columns. These techniques are essential for preparing clean and reliable datasets before performing data analysis or building machine learning models.


Key Takeaways

  • isnull() detects missing values.
  • notnull() identifies non-missing values.
  • dropna() removes rows or columns containing missing values.
  • fillna() replaces missing values with custom values.
  • mean() and median() are commonly used for numerical imputation.
  • ffill() performs forward filling.
  • bfill() performs backward filling.
  • replace() can convert specific values into NaN.
  • drop_duplicates() removes duplicate records after cleaning.
  • Proper handling of missing values improves data quality and model accuracy.

Frequently Asked Questions (FAQs)

1. How do you detect missing values in Pandas?

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. How do you replace missing values with the column mean?

df["Salary"] = df["Salary"].fillna(
    df["Salary"].mean()
)

6. What is the difference between ffill() and bfill()?

  • ffill() fills missing values using the previous valid value.
  • bfill() fills missing values using the next valid value.

7. How do you check whether a DataFrame contains any missing values?

df.isnull().values.any()

8. Why is handling missing data important in Pandas?

Handling missing values improves data quality, ensures accurate analysis, prevents errors during model training, and produces more reliable insights in business intelligence, reporting, and machine learning projects.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top