Counting duplicates helps you find repeated data in your table. This is useful to check data quality or understand patterns.
Counting duplicates in Pandas
DataFrame.duplicated(subset=None, keep='first') DataFrame.drop_duplicates(subset=None, keep='first') Series.value_counts()
duplicated() marks rows that are duplicates.
value_counts() counts how many times each unique value appears.
df.duplicated()
df['column'].value_counts()df.duplicated(subset=['col1', 'col2'], keep=False)
This code creates a small table with names and ages. It marks duplicate rows and counts how many times each name appears.
import pandas as pd data = {'Name': ['Anna', 'Bob', 'Anna', 'Mike', 'Bob', 'Anna'], 'Age': [25, 30, 25, 22, 30, 25]} df = pd.DataFrame(data) # Find duplicate rows duplicates = df.duplicated() print('Duplicate rows marked as True:') print(duplicates) # Count how many times each name appears name_counts = df['Name'].value_counts() print('\nCount of each name:') print(name_counts)
Use keep=False in duplicated() to mark all duplicates as True, not just later ones.
value_counts() works on Series (one column) and on DataFrames to count unique row combinations (pandas 1.1+).
Counting duplicates helps find repeated data in tables.
duplicated() marks duplicate rows as True or False.
value_counts() counts how often each value appears in a column.
Practice
What does the duplicated() function in pandas do?
Solution
Step 1: Understand the purpose of duplicated()
Theduplicated()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
Unlikedrop_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 CQuick Check:
duplicated() = True/False mask [OK]
- Thinking duplicated() removes duplicates
- Confusing duplicated() with value_counts()
- Assuming duplicated() sorts data
Which of the following is the correct syntax to count duplicate rows in a pandas DataFrame df?
?
Solution
Step 1: Identify correct function names
The correct function to find duplicates isduplicated(). There is noduplicate()orcount_duplicates()in pandas.Step 2: Count duplicates using sum()
duplicated()returns a boolean Series. Usingsum()counts how many True values (duplicates) exist.Final Answer:
df.duplicated().sum() -> Option AQuick Check:
duplicated() + sum() = count duplicates [OK]
- Using wrong function names like duplicate()
- Trying to count duplicates with value_counts() directly
- Using count() instead of sum() on boolean Series
Given the DataFrame df below, what is the output of df.duplicated().sum()?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 2, 3, 1], 'B': ['x', 'y', 'y', 'z', 'x']})
print(df.duplicated().sum())Solution
Step 1: Identify duplicate rows
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), soduplicated()marks these two rows True. Summing gives 2.Final Answer:
2 -> Option BQuick Check:
Two duplicate rows found = 2 [OK]
- Counting all duplicates including first occurrence
- Confusing duplicated() with value_counts()
- Ignoring that duplicated() marks only later duplicates
What is wrong with this code to count duplicates in DataFrame df?
duplicates = df.duplicated.count()
Solution
Step 1: Check method call syntax
duplicatedis a method and must be called with parentheses:duplicated().Step 2: Understand method chaining
Without parentheses,df.duplicated.count()tries to accesscountattribute of the method itself, causing error. Correct isdf.duplicated().count(), but this counts all rows, not duplicates.Final Answer:
duplicated is a method and needs parentheses before count() -> Option AQuick Check:
Call duplicated() with () before count() [OK]
- Forgetting parentheses on duplicated()
- Using count() instead of sum() to count duplicates
- Assuming duplicated is a property, not a method
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?
?
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 DQuick Check:
value_counts() + filter > 1 = repeated categories [OK]
- Using duplicated() on a column returns booleans, not counts
- Trying to call non-existent count_duplicates()
- Using count() on duplicated() output incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
