📖 Scenario: You work in a small store that keeps track of sales data. Sometimes, the same sale is recorded twice by mistake. You want to clean the data by removing these duplicate sales records.
🎯 Goal: You will create a sales data table, set a rule to identify duplicates, remove the duplicate rows, and then show the cleaned data.
📋 What You'll Learn
Create a pandas DataFrame with sales data including duplicates
Set a variable to specify which columns to check for duplicates
Use drop_duplicates to remove duplicate rows based on the specified columns
Print the cleaned DataFrame
💡 Why This Matters
🌍 Real World
Cleaning duplicate records is a common task in data analysis to ensure accurate reports and decisions.
💼 Career
Data analysts and scientists often need to remove duplicate data entries before analysis to avoid errors.
Progress0 / 4 steps
1
Create the sales data table
Create a pandas DataFrame called sales_data with these exact columns and rows: SaleID: [101, 102, 103, 102, 104] Product: ['Apple', 'Banana', 'Apple', 'Banana', 'Orange'] Quantity: [5, 3, 5, 3, 2]
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column values.
2
Set columns to check for duplicates
Create a variable called check_columns and set it to a list containing the column names 'SaleID' and 'Product'.
Data Analysis Python
Hint
Make a list with the two column names exactly as strings.
3
Remove duplicate sales records
Create a new DataFrame called cleaned_sales by removing duplicate rows from sales_data using drop_duplicates with the subset set to check_columns.
Data Analysis Python
Hint
Use drop_duplicates with the subset parameter set to the list check_columns.
4
Show the cleaned sales data
Print the cleaned_sales DataFrame to display the sales data after removing duplicates.
Data Analysis Python
Hint
Use print(cleaned_sales) to show the DataFrame.
Practice
(1/5)
1. What does the drop_duplicates() function do in a pandas DataFrame?
easy
A. Fills missing values with zeros
B. Removes repeated rows based on all columns by default
C. Sorts the DataFrame rows in ascending order
D. Changes the data type of columns
Solution
Step 1: Understand the function purpose
drop_duplicates() is used to remove repeated rows in a DataFrame.
Step 2: Default behavior
By default, it checks all columns to find duplicates and keeps the first occurrence.
Final Answer:
Removes repeated rows based on all columns by default -> Option B
Hint: Use subset to check duplicates on specific columns [OK]
Common Mistakes:
Not using subset and dropping all duplicates
Keeping all duplicates instead of first
Confusing which rows are dropped
4. What is wrong with this code snippet to remove duplicates from DataFrame df?
df.drop_duplicates(inplace=True)
After running it, duplicates still appear in df. Why?
medium
A. The DataFrame has no duplicates to remove
B. drop_duplicates() does not modify the original DataFrame even with inplace=True
C. The DataFrame was not reassigned and inplace=True is ignored
D. The inplace parameter is misspelled
Solution
Step 1: Check inplace parameter usage
inplace=True modifies the original DataFrame, so no reassignment needed.
Step 2: Consider DataFrame content
If duplicates still appear, likely the DataFrame had no duplicates to remove.
Final Answer:
The DataFrame has no duplicates to remove -> Option A
Quick Check:
inplace=True modifies original; no duplicates means no change [OK]
Hint: Check if DataFrame actually has duplicates before drop_duplicates [OK]
Common Mistakes:
Assuming inplace=True does nothing
Thinking drop_duplicates always changes data
Not verifying if duplicates exist
5. You have a DataFrame df with columns ['ID', 'Name', 'Score']. Some rows have duplicate ID values but different Score. How do you keep only the row with the highest Score for each ID?
hard
A. Use df.drop_duplicates(subset=['Score'])
B. Use df.drop_duplicates(subset=['ID']) without sorting
C. Use df.drop_duplicates(keep='last') without sorting
D. Use df.sort_values('Score', ascending=False).drop_duplicates(subset=['ID'])
Solution
Step 1: Sort DataFrame by Score descending
Sorting by 'Score' descending puts highest scores first for each ID.
Step 2: Drop duplicates by ID keeping first
After sorting, dropping duplicates by 'ID' keeps the first (highest score) row per ID.
Final Answer:
df.sort_values('Score', ascending=False).drop_duplicates(subset=['ID']) -> Option D
Quick Check:
Sort then drop duplicates keeps highest score per ID [OK]
Hint: Sort by score descending before drop_duplicates to keep max [OK]
Common Mistakes:
Not sorting before dropping duplicates
Dropping duplicates on wrong column
Using keep='last' without sorting
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.