duplicated() for finding duplicates in Pandas - Time & Space Complexity
We want to understand how the time needed to find duplicates grows as the data gets bigger.
How does pandas check each row to see if it appeared before?
Analyze the time complexity of the following code snippet.
import pandas as pd
data = pd.DataFrame({
'A': [1, 2, 2, 3, 4, 4, 4],
'B': ['x', 'y', 'y', 'z', 'x', 'x', 'x']
})
duplicates = data.duplicated()
print(duplicates)
This code creates a small table and finds which rows are duplicates of earlier rows.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Hashing each row and checking if it was seen before in a dictionary.
- How many times: Once for each row (O(1) average time per check).
As the number of rows grows, the work to find duplicates grows in proportion to the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 operations |
| 100 | About 100 operations |
| 1000 | About 1,000 operations |
Pattern observation: Doubling the number of rows roughly doubles the work needed.
Time Complexity: O(n)
This means the time to find duplicates grows linearly as the data gets bigger, thanks to efficient hashing.
[X] Wrong: "Finding duplicates requires checking each row against all previous rows, so O(n²)."
[OK] Correct: Pandas uses hashing (dictionary lookups) for O(1) average checks per row, making it O(n) total.
Understanding how duplicate detection scales helps you explain data processing limits and choose better methods when needed.
"What if we only checked duplicates in one column instead of all columns? How would the time complexity change?"
Practice
duplicated() function in pandas do?Solution
Step 1: Understand the purpose of
Theduplicated()duplicated()function checks each row and marks it as True if it is a duplicate of a previous row.Step 2: Differentiate from other functions
Unlike functions that remove duplicates or count unique rows,duplicated()only flags duplicates without changing the DataFrame.Final Answer:
It identifies duplicate rows in a DataFrame. -> Option AQuick Check:
duplicated()= identifies duplicates [OK]
- Confusing duplicated() with drop_duplicates()
- Thinking duplicated() sorts data
- Assuming duplicated() counts unique rows
df?Solution
Step 1: Recall the exact function name
The correct pandas function to find duplicate rows isduplicated(), not any other variation.Step 2: Verify syntax correctness
The function is called on the DataFrame object asdf.duplicated()with parentheses.Final Answer:
df.duplicated() -> Option AQuick Check:
Correct function name = df.duplicated() [OK]
- Misspelling the function name
- Omitting parentheses after duplicated
- Using non-existent functions like duplicates()
df below, what is the output of df.duplicated()?
import pandas as pd
data = {'A': [1, 2, 2, 3], 'B': ['x', 'y', 'y', 'z']}
df = pd.DataFrame(data)
print(df.duplicated())Solution
Step 1: Identify duplicate rows in the DataFrame
The rows are: 0: (1, 'x'), 1: (2, 'y'), 2: (2, 'y'), 3: (3, 'z'). Row 2 is a duplicate of row 1.Step 2: Understand duplicated() output
duplicated()returns False for the first occurrence and True for duplicates. So, rows 0,1,3 are False; row 2 is True.Final Answer:
[False, False, True, False] -> Option BQuick Check:
First duplicate row marked True = [False, False, True, False] [OK]
- Marking first occurrence as True
- Confusing row indices with values
- Assuming all duplicates are True regardless of position
df?
duplicates = df.duplicated['A'] print(duplicates)
Solution
Step 1: Check function call syntax
The code uses square brackets[]instead of parentheses()to call theduplicated()method, which is incorrect syntax.Step 2: Correct way to specify columns
To check duplicates based on column 'A', the correct syntax isdf.duplicated(subset=['A'])with parentheses.Final Answer:
Using square brackets instead of parentheses to call duplicated() -> Option DQuick Check:
Call methods with () not [] [OK]
- Using [] instead of () to call functions
- Trying to access duplicated as an attribute
- Not using subset parameter for specific columns
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?Solution
Step 1: Use subset parameter to specify columns
To check duplicates based on specific columns, use thesubsetparameter with a list of column names.Step 2: Validate correct syntax
The correct syntax isdf.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 CQuick Check:
Use subset=['col1', 'col2'] to check specific columns [OK]
- Using wrong parameter name like columns instead of subset
- Passing list without parameter name
- Confusing keep parameter with subset
Ready to prove it?
Pass the test and this pattern moves unknown
