By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Data cleaning is the process of identifying and correcting (or removing) errors, inconsistencies, and inaccuracies in datasets to improve their quality. You use it because raw data is almost never ready for analysis—dirty data leads to wrong insights, wasted resources, and flawed models.
Clean data meets these criteria: - Accuracy: Values match real-world truth (e.g., a customer’s age is 35, not 350).- Completeness: No missing values (e.g., all rows have a "date of birth" field).- Consistency: Data aligns across systems (e.g., "USA" vs. "United States" in the same column).- Uniqueness: No duplicates (e.g., the same customer listed twice).- Validity: Data conforms to rules (e.g., dates in YYYY-MM-DD format).- Timeliness: Data is up-to-date (e.g., stock prices from last week, not last year).
YYYY-MM-DD
NaN
int
float
datetime
Data cleaning is iterative. Here’s a typical flow:
pandas
df.describe()
df.isnull().sum()
Example Workflow in Python:
import pandas as pd # Load data df = pd.read_csv("dirty_data.csv") # Inspect print(df.info()) # Check data types and missing values print(df.duplicated().sum()) # Count duplicates # Clean df.drop_duplicates(inplace=True) # Remove duplicates df["date"] = pd.to_datetime(df["date"], errors="coerce") # Standardize dates df["age"].fillna(df["age"].median(), inplace=True) # Impute missing ages # Validate assert df["age"].isnull().sum() == 0 # Ensure no missing ages df.to_csv("clean_data.csv", index=False) # Export
numpy
Goal: Clean a dataset with missing values, duplicates, and inconsistent formats.
Load the data: python import pandas as pd df = pd.read_csv("titanic.csv")
python import pandas as pd df = pd.read_csv("titanic.csv")
Inspect: python print(df.head()) # Preview first 5 rows print(df.isnull().sum()) # Count missing values per column print(df.dtypes) # Check data types
python print(df.head()) # Preview first 5 rows print(df.isnull().sum()) # Count missing values per column print(df.dtypes) # Check data types
Clean:
python df.drop_duplicates(inplace=True)
python # Fill missing ages with median df["Age"].fillna(df["Age"].median(), inplace=True) # Drop rows where Embarked is missing (only 2 rows) df.dropna(subset=["Embarked"], inplace=True)
python df["Sex"] = df["Sex"].str.lower() # Convert to lowercase
Convert data types: python df["Fare"] = df["Fare"].astype(float) # Ensure Fare is float
python df["Fare"] = df["Fare"].astype(float) # Ensure Fare is float
Validate: python print(df.isnull().sum()) # Should show 0 missing values print(df["Sex"].unique()) # Should show ["male", "female"]
python print(df.isnull().sum()) # Should show 0 missing values print(df["Sex"].unique()) # Should show ["male", "female"]
Export: python df.to_csv("titanic_clean.csv", index=False)
python df.to_csv("titanic_clean.csv", index=False)
Expected Outcome: A cleaned CSV file with no duplicates, standardized formats, and no missing values in critical columns.
README
df.info()
python def standardize_dates(df, column): df[column] = pd.to_datetime(df[column], errors="coerce") return df
is_missing
python assert df["age"].between(0, 120).all() # Ensure ages are realistic
data_v1.csv
data_v2_cleaned.csv
You’re cleaning a dataset of employee salaries and notice 10% of values are missing. What’s the best approach? A) Drop all rows with missing salaries.B) Fill missing salaries with 0.C) Fill missing salaries with the median salary.D) Ignore the missing values and proceed.
Correct Answer: CExplanation: The median is robust to outliers and preserves the distribution. Dropping 10% of data (A) loses information, filling with 0 (B) distorts the data, and ignoring (D) risks model failures.Why the Distractors Are Tempting: - A: Seems simple but loses valuable data.- B: 0 is a valid salary, but it’s not a realistic replacement.- D: Ignoring missing data is never a good idea in analysis.
A dataset has a "date" column with values like "01/12/2023" and "2023-12-01". How should you standardize this? A) Convert all dates to the format "DD-MM-YYYY".B) Convert all dates to datetime objects in Python.C) Leave the dates as-is since they’re readable.D) Replace all dates with the current date.
Correct Answer: BExplanation: datetime objects allow for consistent parsing, sorting, and calculations. Standardizing to a string format (A) is less flexible, leaving as-is (C) causes parsing errors, and replacing with the current date (D) corrupts the data.Why the Distractors Are Tempting: - A: Seems logical but limits functionality (e.g., can’t sort chronologically easily).- C: Avoids work but leads to inconsistencies.- D: Quick fix but destroys data integrity.
You’re cleaning a dataset and find duplicate rows. What’s the first step? A) Remove all duplicates immediately.B) Check if the duplicates are valid (e.g., same customer with multiple orders).C) Keep the first duplicate and drop the rest.D) Replace duplicates with the average of their values.
Correct Answer: BExplanation: Not all duplicates are errors. For example, the same customer might have multiple orders. Always investigate before deleting.Why the Distractors Are Tempting: - A: Assumes all duplicates are bad, which isn’t true.- C: Arbitrarily keeps the first row, which may not be correct.- D: Makes no sense for most datasets (e.g., averaging customer IDs).
fuzzywuzzy
pydantic
great_expectations
dplyr
tidyr
janitor
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.