The DataFrame has rows with columns A and B: (1,4), (2,5), (1,4), (3,6).
Step 2: Identify duplicates on columns 'A' and 'B'
The third row (index 2) duplicates the first row (index 0) on columns A and B.
Final Answer:
[False, False, True, False] -> Option C
Quick Check:
Only third row is duplicate on A and B [OK]
Hint: Check repeated (A,B) pairs for True in duplicated() output [OK]
Common Mistakes:
Assuming duplicates on all columns
Marking first occurrence as duplicate
Confusing index positions
4. You run this code:
df.duplicated(subset=['A', 'B'], keep='last')
but get unexpected results. What is a common mistake causing this?
medium
A. Forgetting to pass a list to subset causes an error.
B. Using keep='last' marks the last occurrence as False, others as True.
C. Using keep='last' marks the first duplicates as True, not the last.
D. The duplicated() method does not accept keep parameter.
Solution
Step 1: Understand keep parameter behavior
keep='last' marks all duplicates except the last occurrence as True.
Step 2: Identify common confusion
Many expect it to mark the last duplicate as True, but it actually keeps the last as False (not duplicate).
Final Answer:
Using keep='last' marks the last occurrence as False, others as True. -> Option B
Quick Check:
keep='last' keeps last duplicate False [OK]
Hint: Remember keep='last' keeps last duplicate False, others True [OK]
Common Mistakes:
Thinking keep='last' marks last duplicate True
Passing subset as string instead of list
Believing duplicated() lacks keep parameter
5. You have a DataFrame df with columns 'Name', 'Age', 'City'. You want to remove rows where 'Name' and 'City' are duplicates but keep the first occurrence. Which code correctly does this?
hard
A. df[~df.duplicated(subset=['Name', 'City'], keep='first')]
B. df = df.duplicated(subset=['Name', 'City'], keep='first')
C. df = df.drop_duplicates(columns=['Name', 'City'], keep='last')
D. df = df.drop_duplicates(subset=['Name', 'City'], keep='first')
Solution
Step 1: Identify method to remove duplicates
Use drop_duplicates() to remove duplicate rows.
Step 2: Use subset and keep parameters correctly
Pass subset=['Name', 'City'] and keep='first' to keep first occurrence and remove others.
Step 3: Check options
df = df.drop_duplicates(subset=['Name', 'City'], keep='first') uses correct method and parameters. df[~df.duplicated(subset=['Name', 'City'], keep='first')] uses duplicated() with negation but does not assign back to df. df = df.drop_duplicates(columns=['Name', 'City'], keep='last') uses wrong parameter name. df = df.duplicated(subset=['Name', 'City'], keep='first') returns boolean Series, not DataFrame.
Final Answer:
df = df.drop_duplicates(subset=['Name', 'City'], keep='first') -> Option D
Quick Check:
drop_duplicates with subset and keep='first' removes duplicates [OK]
Hint: Use drop_duplicates(subset=[...], keep='first') to remove duplicates [OK]
Common Mistakes:
Using duplicated() instead of drop_duplicates() to remove rows
Passing columns instead of subset
Not assigning result back to df
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.