Data cleaning is one of the most important steps in data analysis. Real-world datasets often contain missing values, duplicate records, incorrect data types, inconsistent formatting, and invalid entries. Pandas provides powerful functions to identify, clean, and transform dirty data into a structured format suitable for analysis. In this chapter, you’ll learn how to handle missing values, remove duplicates, replace incorrect values, and clean datasets using practical examples. Pandas Data Cleaning practice questions with solutions help to understand the concepts.
1. Python Program to Check Missing Values in a DataFrame
Problem Statement
Write a Python program to identify missing values in a Pandas DataFrame.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Marks": [85, np.nan, 78, np.nan]
}
df = pd.DataFrame(data)
print(df.isnull())
Sample Output
Name Marks
0 False False
1 False True
2 False False
3 False True
Explanation
The isnull() function checks every value in the DataFrame and returns True where a value is missing (NaN).
Concepts Covered
isnull()- Missing Values
- Data Cleaning
2. Python Program to Count Missing Values in Each Column
Problem Statement
Write a Python program to count the total number of missing values in every column.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Age": [20, 21, np.nan, 22],
"Marks": [85, np.nan, 78, np.nan]
}
df = pd.DataFrame(data)
print(df.isnull().sum())
Sample Output
Name 0
Age 1
Marks 2
dtype: int64
Explanation
The sum() function counts the number of True values returned by isnull(), giving the total missing values in each column.
Concepts Covered
isnull()sum()- Missing Value Count
3. Python Program to Remove Rows Containing Missing Values
Problem Statement
Write a Python program to remove all rows containing missing values.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Marks": [85, np.nan, 78, 92]
}
df = pd.DataFrame(data)
clean_df = df.dropna()
print(clean_df)
Sample Output
Name Marks
0 Rahul 85.0
2 Priya 78.0
3 Sneha 92.0
Explanation
The dropna() function removes rows that contain one or more missing values.
Concepts Covered
dropna()- Missing Values
- Row Removal
4. Python Program to Fill Missing Values with a Constant
Problem Statement
Write a Python program to replace all missing values with 0.
Python Solution
import pandas as pd
import numpy as np
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Marks": [85, np.nan, 78, np.nan]
}
df = pd.DataFrame(data)
df["Marks"] = df["Marks"].fillna(0)
print(df)
Sample Output
Name Marks
0 Rahul 85.0
1 Aman 0.0
2 Priya 78.0
3 Sneha 0.0
Explanation
The fillna() function replaces missing values with a specified constant value.
Concepts Covered
fillna()- Missing Value Replacement
- Data Cleaning
5. Python Program to Fill Missing Values with the Column Mean
Problem Statement
Write a Python program to replace missing values with the average of the column.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [85, np.nan, 78, 92]
}
df = pd.DataFrame(data)
mean_value = df["Marks"].mean()
df["Marks"] = df["Marks"].fillna(mean_value)
print(df)
Sample Output
Marks
0 85.000000
1 85.000000
2 78.000000
3 92.000000
Explanation
The mean() function calculates the average value of the column, and fillna() replaces missing values with that average.
Concepts Covered
fillna()mean()- Missing Value Imputation
6. Python Program to Fill Missing Values with the Median
Problem Statement
Write a Python program to replace missing values in a DataFrame with the median value of the column.
Python Solution
import pandas as pd
import numpy as np
data = {
"Marks": [85, np.nan, 78, 92, 88]
}
df = pd.DataFrame(data)
median_value = df["Marks"].median()
df["Marks"] = df["Marks"].fillna(median_value)
print(df)
Sample Output
Marks
0 85.0
1 86.5
2 78.0
3 92.0
4 88.0
Explanation
The median() function calculates the middle value of the column. It is often preferred over the mean when the dataset contains outliers.
Concepts Covered
median()fillna()- Missing Value Imputation
7. Python Program to Fill Missing Values with the Mode
Problem Statement
Write a Python program to replace missing values with the most frequently occurring value in a column.
Python Solution
import pandas as pd
import numpy as np
data = {
"City": ["Delhi", "Mumbai", np.nan, "Delhi", "Delhi"]
}
df = pd.DataFrame(data)
mode_value = df["City"].mode()[0]
df["City"] = df["City"].fillna(mode_value)
print(df)
Sample Output
City
0 Delhi
1 Mumbai
2 Delhi
3 Delhi
4 Delhi
Explanation
The mode() function returns the most frequently occurring value. Missing values are replaced using fillna().
Concepts Covered
mode()fillna()- Categorical Data Cleaning
8. Python Program to Remove Duplicate Rows
Problem Statement
Write a Python program to remove duplicate rows from a DataFrame.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "Aman", "Rahul", "Sneha"],
"Marks": [85, 90, 85, 88]
}
df = pd.DataFrame(data)
clean_df = df.drop_duplicates()
print(clean_df)
Sample Output
Name Marks
0 Rahul 85
1 Aman 90
3 Sneha 88
Explanation
The drop_duplicates() function removes duplicate rows while keeping the first occurrence.
Concepts Covered
drop_duplicates()- Duplicate Records
- Data Cleaning
9. Python Program to Check Duplicate Rows
Problem Statement
Write a Python program to identify duplicate rows in a DataFrame.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "Aman", "Rahul", "Sneha"],
"Marks": [85, 90, 85, 88]
}
df = pd.DataFrame(data)
print(df.duplicated())
Sample Output
0 False
1 False
2 True
3 False
dtype: bool
Explanation
The duplicated() function returns True for rows that are duplicates of previous rows.
Concepts Covered
duplicated()- Duplicate Detection
- Boolean Output
10. Python Program to Replace Specific Values in a DataFrame
Problem Statement
Write a Python program to replace all occurrences of “Delhi” with “New Delhi” in a DataFrame.
Python Solution
import pandas as pd
data = {
"City": ["Delhi", "Mumbai", "Delhi", "Pune"]
}
df = pd.DataFrame(data)
df["City"] = df["City"].replace("Delhi", "New Delhi")
print(df)
Sample Output
City
0 New Delhi
1 Mumbai
2 New Delhi
3 Pune
Explanation
The replace() function substitutes one value with another throughout the DataFrame or selected columns.
Concepts Covered
replace()- Value Replacement
- Data Cleaning
11. Python Program to Remove Leading and Trailing Spaces from a Column
Problem Statement
Write a Python program to remove leading and trailing spaces from the Name column.
Python Solution
import pandas as pd
data = {
"Name": [" Rahul ", " Aman", "Priya ", " Sneha "]
}
df = pd.DataFrame(data)
df["Name"] = df["Name"].str.strip()
print(df)
Sample Output
Name
0 Rahul
1 Aman
2 Priya
3 Sneha
Explanation
The str.strip() method removes extra spaces from the beginning and end of each string.
Concepts Covered
str.strip()- String Cleaning
- Data Cleaning
12. Python Program to Convert Text to Lowercase
Problem Statement
Write a Python program to convert all values in the City column to lowercase.
Python Solution
import pandas as pd
data = {
"City": ["Delhi", "MUMBAI", "Jaipur", "PUNE"]
}
df = pd.DataFrame(data)
df["City"] = df["City"].str.lower()
print(df)
Sample Output
City
0 delhi
1 mumbai
2 jaipur
3 pune
Explanation
The str.lower() method converts every string in the selected column to lowercase, ensuring consistency in textual data.
Concepts Covered
str.lower()- String Operations
- Data Standardization
13. Python Program to Convert Text to Uppercase
Problem Statement
Write a Python program to convert all values in the Course column to uppercase.
Python Solution
import pandas as pd
data = {
"Course": ["Python", "Java", "Pandas", "NumPy"]
}
df = pd.DataFrame(data)
df["Course"] = df["Course"].str.upper()
print(df)
Sample Output
Course
0 PYTHON
1 JAVA
2 PANDAS
3 NUMPY
Explanation
The str.upper() method converts every string in the selected column to uppercase.
Concepts Covered
str.upper()- Text Formatting
- Data Cleaning
14. Python Program to Rename Multiple Columns
Problem Statement
Write a Python program to rename multiple columns in a DataFrame.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "Aman"],
"Marks": [85, 90]
}
df = pd.DataFrame(data)
df.rename(
columns={
"Name": "Student Name",
"Marks": "Score"
},
inplace=True
)
print(df)
Sample Output
Student Name Score
0 Rahul 85
1 Aman 90
Explanation
The rename() function changes one or more column names without affecting the underlying data.
Concepts Covered
rename()- Column Renaming
- DataFrame Modification
15. Python Program to Change the Data Type of a Column
Problem Statement
Write a Python program to convert the Marks column from string to integer.
Python Solution
import pandas as pd
data = {
"Marks": ["85", "90", "78", "88"]
}
df = pd.DataFrame(data)
df["Marks"] = df["Marks"].astype(int)
print(df)
print(df.dtypes)
Sample Output
Marks
0 85
1 90
2 78
3 88
Marks int64
dtype: object
Explanation
The astype() function converts the data type of a column. It is commonly used to convert strings into numeric values before performing calculations.
Concepts Covered
astype()- Data Type Conversion
- Data Cleaning
16. Python Program to Remove Rows with Empty Strings
Problem Statement
Write a Python program to remove rows where the Name column contains an empty string.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "", "Priya", "Sneha"],
"Marks": [85, 90, 78, 88]
}
df = pd.DataFrame(data)
clean_df = df[df["Name"] != ""]
print(clean_df)
Sample Output
Name Marks
0 Rahul 85
2 Priya 78
3 Sneha 88
Explanation
This program filters out rows where the Name column contains an empty string, keeping only valid records.
Concepts Covered
- Boolean Indexing
- Empty String Removal
- Data Cleaning
17. Python Program to Remove Outliers Using a Condition
Problem Statement
Write a Python program to remove records where the Marks value is greater than 100.
Python Solution
import pandas as pd
data = {
"Name": ["Rahul", "Aman", "Priya", "Sneha"],
"Marks": [85, 120, 78, 95]
}
df = pd.DataFrame(data)
clean_df = df[df["Marks"] <= 100]
print(clean_df)
Sample Output
Name Marks
0 Rahul 85
2 Priya 78
3 Sneha 95
Explanation
Sometimes datasets contain invalid values that fall outside an acceptable range. Boolean indexing can be used to remove such outlier records before analysis.
Concepts Covered
- Boolean Filtering
- Outlier Removal
- Data Validation
Chapter Summary
In this chapter, you learned how to clean datasets using Pandas. You explored techniques for identifying and handling missing values, filling missing data with mean, median, and mode, removing duplicate records, replacing incorrect values, trimming extra spaces, standardizing text using uppercase and lowercase conversion, renaming columns, changing data types, removing empty strings, and filtering invalid records. These data cleaning techniques are essential for preparing high-quality datasets before performing analysis or building machine learning models.
Key Takeaways
- Use
isnull()andisnull().sum()to detect missing values. - Remove missing data using
dropna(). - Replace missing values using
fillna(). - Mean, median, and mode can be used for missing value imputation.
- Use
drop_duplicates()to remove duplicate records. - Detect duplicates using
duplicated(). - Replace incorrect values using
replace(). - Clean text using
str.strip(),str.lower(), andstr.upper(). - Convert data types using
astype(). - Filter invalid records using Boolean indexing.
Frequently Asked Questions (FAQs)
1. What is data cleaning in Pandas?
Data cleaning is the process of identifying and correcting missing, duplicate, inconsistent, or invalid data before analysis.
2. How do you identify missing values?
Use the isnull() function.
print(df.isnull())
3. How do you remove rows containing missing values?
Use the dropna() function.
clean_df = df.dropna()
4. Which function replaces missing values?
Use the fillna() function.
df["Marks"] = df["Marks"].fillna(0)
5. How do you remove duplicate rows?
Use the drop_duplicates() function.
clean_df = df.drop_duplicates()
6. How do you convert a column to a different data type?
Use the astype() function.
df["Marks"] = df["Marks"].astype(int)
7. How do you remove extra spaces from text?
Use the str.strip() method.
df["Name"] = df["Name"].str.strip()
8. Why is data cleaning important?
Data cleaning improves data quality by removing errors, inconsistencies, duplicates, and invalid values. Clean data leads to more accurate analysis, reliable reports, and better machine learning model performance.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
