Hint: First occurrence is False, duplicates after are True [OK]
Common Mistakes:
Marking first occurrence as True
Confusing row indices with values
Assuming all duplicates are True regardless of position
4. What is wrong with this code snippet to find duplicates in column 'A' of DataFrame df?
duplicates = df.duplicated['A']
print(duplicates)
medium
A. duplicated() cannot check specific columns
B. Missing import statement for pandas
C. The print statement is incorrect
D. Using square brackets instead of parentheses to call duplicated()
Solution
Step 1: Check function call syntax
The code uses square brackets [] instead of parentheses () to call the duplicated() method, which is incorrect syntax.
Step 2: Correct way to specify columns
To check duplicates based on column 'A', the correct syntax is df.duplicated(subset=['A']) with parentheses.
Final Answer:
Using square brackets instead of parentheses to call duplicated() -> Option D
Quick Check:
Call methods with () not [] [OK]
Hint: Call duplicated() with parentheses, not square brackets [OK]
Common Mistakes:
Using [] instead of () to call functions
Trying to access duplicated as an attribute
Not using subset parameter for specific columns
5. You have a DataFrame df with columns 'Name', 'Age', and 'City'. You want to find duplicates only based on 'Name' and 'City', ignoring 'Age'. Which code correctly finds these duplicates?
hard
A. df.duplicated(['Name', 'City'], keep=True)
B. df.duplicated(columns=['Name', 'City'])
C. df.duplicated(subset=['Name', 'City'])
D. df.duplicated('Name', 'City')
Solution
Step 1: Use subset parameter to specify columns
To check duplicates based on specific columns, use the subset parameter with a list of column names.
Step 2: Validate correct syntax
The correct syntax is df.duplicated(subset=['Name', 'City']). df.duplicated(columns=['Name', 'City']) uses a wrong parameter name (columns), and options A and C use invalid syntax.
Final Answer:
df.duplicated(subset=['Name', 'City']) -> Option C
Quick Check:
Use subset=['col1', 'col2'] to check specific columns [OK]
Hint: Use subset parameter to check duplicates on chosen columns [OK]
Common Mistakes:
Using wrong parameter name like columns instead of subset
Passing list without parameter name
Confusing keep parameter with subset
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.