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.