📖 Scenario: Imagine you work in a store that keeps track of customer purchases. Sometimes, the same purchase gets recorded twice by mistake. This can cause problems when you try to understand how many unique purchases were made.
🎯 Goal: You will learn how to find duplicate entries in a list of purchases using pandas. This helps keep data clean and accurate.
📋 What You'll Learn
Create a pandas DataFrame with purchase data
Set a variable to count duplicates
Use pandas to find duplicate rows
Print the number of duplicate purchases
💡 Why This Matters
🌍 Real World
Duplicate detection helps businesses keep their data clean and accurate, avoiding mistakes in reports and decisions.
💼 Career
Data analysts and scientists often clean data by finding and removing duplicates to ensure trustworthy analysis.
Progress0 / 4 steps
1
Create purchase data
Create a pandas DataFrame called purchases with these exact rows: {'Customer': ['Alice', 'Bob', 'Alice', 'David', 'Bob'], 'Item': ['Apple', 'Banana', 'Apple', 'Carrot', 'Banana']}
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of entries.
2
Set duplicate counter
Create a variable called duplicate_count and set it to 0 to prepare for counting duplicates.
Pandas
Hint
Just write duplicate_count = 0 to start counting duplicates.
3
Find duplicates in purchases
Use purchases.duplicated() to find duplicate rows and assign the sum of duplicates to duplicate_count.
Pandas
Hint
Use purchases.duplicated() to get a boolean Series of duplicates, then sum it to count.
4
Print number of duplicates
Print the text "Number of duplicate purchases:" followed by the value of duplicate_count.
Pandas
Hint
Use print("Number of duplicate purchases:", duplicate_count) to show the result.
Practice
(1/5)
1. Why is detecting duplicates important in data analysis using pandas?
easy
A. Duplicates help in adding more rows to the dataset.
B. Duplicates make the data look colorful in plots.
C. Duplicates can cause incorrect results and bias in analysis.
D. Duplicates increase the speed of data processing.
Solution
Step 1: Understand the impact of duplicates on data
Duplicates repeat the same information, which can skew calculations like averages or counts.
Step 2: Recognize the importance of accuracy
Removing duplicates ensures the analysis reflects true data without repeated bias.
Final Answer:
Duplicates can cause incorrect results and bias in analysis. -> Option C
Quick Check:
Duplicates cause bias [OK]
Hint: Duplicates distort results; always check before analysis [OK]
Common Mistakes:
Thinking duplicates speed up processing
Believing duplicates add useful new data
Ignoring duplicates in analysis
2. Which pandas function is used to find duplicate rows in a DataFrame?
easy
A. df.duplicate()
B. df.duplicated()
C. df.find_duplicates()
D. df.get_duplicates()
Solution
Step 1: Recall pandas functions for duplicates
The correct function to detect duplicates is duplicated(), which returns a boolean Series.
Step 2: Verify function names
Other options like duplicate(), find_duplicates(), and get_duplicates() do not exist in pandas.
Final Answer:
df.duplicated() -> Option B
Quick Check:
Correct function is duplicated() [OK]
Hint: Remember: duplicated() returns True for duplicate rows [OK]
Common Mistakes:
Using non-existent functions like duplicate()
Confusing duplicated() with drop_duplicates()
Misspelling the function name
3. Given the DataFrame:
import pandas as pd
data = {'Name': ['Anna', 'Bob', 'Anna', 'Cody'], 'Age': [25, 30, 25, 22]}
df = pd.DataFrame(data)
print(df.duplicated())
What is the output?
medium
A. [True, True, False, False]
B. [True, False, True, False]
C. [False, True, False, True]
D. [False, False, True, False]
Solution
Step 1: Understand df.duplicated() output
This function marks True for rows that are duplicates of a previous row.
Step 2: Check each row for duplication
Row 0 (Anna,25) is first occurrence -> False. Row 1 (Bob,30) is unique -> False. Row 2 (Anna,25) repeats row 0 -> True. Row 3 (Cody,22) is unique -> False.
Final Answer:
[False, False, True, False] -> Option D
Quick Check:
Third row duplicate = True, others False [OK]
Hint: duplicated() marks True only after first occurrence [OK]
Common Mistakes:
Marking first occurrence as True
Confusing duplicated() with drop_duplicates() output
A. AttributeError: 'DataFrame' object has no attribute 'duplicate'; fix by using df.duplicated()
B. SyntaxError: missing parentheses; fix by adding () after duplicate
C. TypeError: duplicate() requires arguments; fix by passing columns
D. No error; code runs fine
Solution
Step 1: Identify the error type
The error is AttributeError because duplicate() is not a valid pandas method.
Step 2: Correct the method name
The correct method to detect duplicates is duplicated() with a 'd' at the end.
Final Answer:
AttributeError: 'DataFrame' object has no attribute 'duplicate'; fix by using df.duplicated() -> Option A
Quick Check:
Use duplicated(), not duplicate() [OK]
Hint: Check method spelling: duplicated() not duplicate() [OK]
Common Mistakes:
Using duplicate() instead of duplicated()
Assuming no error occurs
Trying to pass arguments to duplicate()
5. You have a DataFrame with columns ['ID', 'Name', 'Score']. Some rows have the same ID but different Score. You want to keep only the first occurrence of each ID and remove duplicates based on ID only. Which code correctly does this?
hard
A. df.drop_duplicates(subset=['ID'], keep='first')
B. df.drop_duplicates(keep='first')
C. df.duplicated(subset=['ID'], keep='first')
D. df.drop_duplicates(subset=['Score'], keep='last')
Solution
Step 1: Understand the goal
We want to remove duplicates based on ID only, keeping the first occurrence.
Step 2: Choose correct method and parameters
drop_duplicates() removes duplicates; subset=['ID'] limits check to ID column; keep='first' keeps first occurrence.
Step 3: Evaluate options
df.drop_duplicates(subset=['ID'], keep='first') matches the requirement. df.drop_duplicates(keep='first') checks all columns, not just ID. df.duplicated(subset=['ID'], keep='first') returns boolean Series, not DataFrame. df.drop_duplicates(subset=['Score'], keep='last') removes duplicates by Score, not ID.
Final Answer:
df.drop_duplicates(subset=['ID'], keep='first') -> Option A
Quick Check:
Drop duplicates by ID, keep first [OK]
Hint: Use subset to specify columns for duplicate check [OK]
Common Mistakes:
Not using subset to limit columns
Using duplicated() instead of drop_duplicates() to remove rows
Keeping last instead of first occurrence
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.