It returns a Boolean Series indicating whether each row is a duplicate of a previous row. True means the row is a duplicate, False means it is unique so far.
Click to reveal answer
beginner
How can you count the total number of duplicate rows in a DataFrame?
Use df.duplicated().sum(). This counts how many rows are duplicates.
Click to reveal answer
intermediate
What does the keep parameter in duplicated() control?
It controls which duplicates to mark as True: - 'first' marks duplicates except the first occurrence - 'last' marks duplicates except the last occurrence - False marks all duplicates as True
Click to reveal answer
intermediate
How do you count duplicates based on specific columns only?
Pass the column names to the subset parameter in duplicated(). Example: df.duplicated(subset=['col1', 'col2']).sum()
Click to reveal answer
beginner
What is the difference between duplicated() and drop_duplicates()?
duplicated() marks duplicate rows with True/False. drop_duplicates() removes duplicate rows from the DataFrame.
Click to reveal answer
Which pandas function returns a Boolean Series marking duplicate rows?
Aduplicated()
Bdrop_duplicates()
Ccount_duplicates()
Dis_duplicate()
✗ Incorrect
duplicated() returns True for duplicate rows, False otherwise.
How do you count the number of duplicate rows in a DataFrame df?
Adf.count_duplicates()
Bdf.drop_duplicates().count()
Cdf.duplicated().sum()
Ddf.duplicated().count()
✗ Incorrect
df.duplicated() returns a Boolean Series; summing it counts duplicates.
What does df.duplicated(keep=False) do?
AMarks all duplicates as True, including first occurrences
BMarks only the first duplicate as True
CMarks only the last duplicate as True
DRemoves duplicates from df
✗ Incorrect
keep=False marks all duplicates as True, not keeping any unique.
To check duplicates based on columns 'A' and 'B' only, which is correct?
Adf.duplicated(['A', 'B'])
Bdf.duplicated(columns=['A', 'B'])
Cdf.duplicated(cols=['A', 'B'])
Ddf.duplicated(subset=['A', 'B'])
✗ Incorrect
The subset parameter specifies columns to check for duplicates.
Which function removes duplicate rows from a DataFrame?
Aduplicated()
Bdrop_duplicates()
Cremove_duplicates()
Ddelete_duplicates()
✗ Incorrect
drop_duplicates() returns a DataFrame without duplicate rows.
Explain how to find and count duplicate rows in a pandas DataFrame.
Think about marking duplicates first, then counting them.
You got /4 concepts.
Describe the difference between duplicated() and drop_duplicates() in pandas.
One marks duplicates, the other removes them.
You got /4 concepts.
Practice
(1/5)
1.
What does the duplicated() function in pandas do?
easy
A. It counts how many times each value appears in a column.
B. It removes all duplicate rows from the DataFrame.
C. It marks duplicate rows as True and unique rows as False.
D. It sorts the DataFrame by duplicate values.
Solution
Step 1: Understand the purpose of duplicated()
The duplicated() function checks each row and marks it True if it is a duplicate of a previous row, otherwise False.
Step 2: Compare with other functions
Unlike drop_duplicates() which removes duplicates, duplicated() only marks them. value_counts() counts values, not duplicates.
Final Answer:
It marks duplicate rows as True and unique rows as False. -> Option C
Quick Check:
duplicated() = True/False mask [OK]
Hint: duplicated() flags duplicates, does not remove them [OK]
Common Mistakes:
Thinking duplicated() removes duplicates
Confusing duplicated() with value_counts()
Assuming duplicated() sorts data
2.
Which of the following is the correct syntax to count duplicate rows in a pandas DataFrame df?
?
easy
A. df.duplicated().sum()
B. df.duplicate().count()
C. df.value_counts().duplicated()
D. df.count_duplicates()
Solution
Step 1: Identify correct function names
The correct function to find duplicates is duplicated(). There is no duplicate() or count_duplicates() in pandas.
Step 2: Count duplicates using sum()
duplicated() returns a boolean Series. Using sum() counts how many True values (duplicates) exist.
Final Answer:
df.duplicated().sum() -> Option A
Quick Check:
duplicated() + sum() = count duplicates [OK]
Hint: Use duplicated() then sum() to count duplicates [OK]
Common Mistakes:
Using wrong function names like duplicate()
Trying to count duplicates with value_counts() directly
Using count() instead of sum() on boolean Series
3.
Given the DataFrame df below, what is the output of df.duplicated().sum()?
Rows 0 and 4 are identical (A=1, B='x'), and rows 1 and 2 are identical (A=2, B='y'). The first occurrence is not counted as duplicate.
Step 2: Count duplicates marked True
Row 2 and row 4 are duplicates (second occurrences), so duplicated() marks these two rows True. Summing gives 2.
Final Answer:
2 -> Option B
Quick Check:
Two duplicate rows found = 2 [OK]
Hint: duplicated() counts second+ occurrences only [OK]
Common Mistakes:
Counting all duplicates including first occurrence
Confusing duplicated() with value_counts()
Ignoring that duplicated() marks only later duplicates
4.
What is wrong with this code to count duplicates in DataFrame df?
duplicates = df.duplicated.count()
medium
A. duplicated is a method and needs parentheses before count()
B. count() cannot be used on duplicated() output
C. duplicated() does not exist in pandas
D. sum() should be used instead of count()
Solution
Step 1: Check method call syntax
duplicated is a method and must be called with parentheses: duplicated().
Step 2: Understand method chaining
Without parentheses, df.duplicated.count() tries to access count attribute of the method itself, causing error. Correct is df.duplicated().count(), but this counts all rows, not duplicates.
Final Answer:
duplicated is a method and needs parentheses before count() -> Option A
Quick Check:
Call duplicated() with () before count() [OK]
Hint: Always use parentheses to call duplicated() method [OK]
Common Mistakes:
Forgetting parentheses on duplicated()
Using count() instead of sum() to count duplicates
Assuming duplicated is a property, not a method
5.
You have a DataFrame df with a column 'Category'. You want to find which categories appear more than once and how many times. Which code snippet correctly does this?
?
hard
A. df['Category'].count_duplicates()
B. df.duplicated('Category').sum()
C. df['Category'].duplicated().count()
D. df['Category'].value_counts()[df['Category'].value_counts() > 1]
Solution
Step 1: Use value_counts() to count category occurrences
value_counts() counts how many times each category appears.
Step 2: Filter categories with counts greater than 1
Using boolean indexing [value_counts() > 1] selects only categories appearing more than once.
Final Answer:
df['Category'].value_counts()[df['Category'].value_counts() > 1] -> Option D