B. Using inplace=True and then printing drop_duplicates() returns original data
C. drop_duplicates() does not have inplace parameter
D. print statement syntax is incorrect
Solution
Step 1: Understand inplace=True effect
Using inplace=True modifies df directly by removing duplicates.
Step 2: Analyze print statement
After the first call, df is already deduplicated. The second call to df.drop_duplicates() is redundant as it returns the same DataFrame since there are no duplicates left.
Final Answer:
Calling drop_duplicates() twice is redundant -> Option A
Quick Check:
Second call redundant after inplace [OK]
Hint: inplace=True changes df; extra drop_duplicates() returns copy [OK]
5. You have a DataFrame df with columns ['Name', 'Age', 'City']. You want to remove duplicates only based on Name and City, keeping the last occurrence. Which code achieves this?
hard
A. df.drop_duplicates(columns=['Name', 'City'], keep='last')
B. df.drop_duplicates(subset=['Name', 'City'], keep='first')
C. df.drop_duplicates(subset=['Name', 'City'], keep='last')
D. df.drop_duplicates(subset=['Age'], keep='last')
Solution
Step 1: Use subset parameter to specify columns
To check duplicates only on 'Name' and 'City', use subset=['Name', 'City'].
Step 2: Set keep='last' to keep last occurrence
keep='last' keeps the last duplicate row, removing earlier ones.
Final Answer:
df.drop_duplicates(subset=['Name', 'City'], keep='last') -> Option C
Quick Check:
subset selects columns; keep='last' keeps last duplicate [OK]
Hint: Use subset for columns, keep='last' to keep last duplicate [OK]
Common Mistakes:
Using 'columns' instead of 'subset' parameter
Forgetting to set keep='last' to keep last occurrence
Selecting wrong columns for subset
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.