Duplicates on specific columns in Pandas - Time & Space Complexity
We want to know how the time needed to find duplicates on certain columns changes as the data grows.
How does the work increase when we have more rows in the table?
Analyze the time complexity of the following code snippet.
import pandas as pd
df = pd.DataFrame({
'A': [1, 2, 2, 3, 4, 4, 4],
'B': ['x', 'y', 'y', 'z', 'x', 'x', 'x'],
'C': [10, 20, 20, 30, 40, 40, 40]
})
duplicates = df.duplicated(subset=['A', 'B'])
This code checks which rows have duplicate values in columns 'A' and 'B'.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each row, hashing values in columns 'A' and 'B' to check if seen before.
- How many times: Once per row.
As the number of rows grows, the work to check duplicates grows roughly in proportion to the number of rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The number of operations grows roughly in a straight line as rows increase.
Time Complexity: O(n)
This means the time to find duplicates grows directly with the number of rows.
[X] Wrong: "Finding duplicates on specific columns takes much longer than checking all columns."
[OK] Correct: The operation mainly depends on the number of rows, not how many columns you check, so checking fewer columns does not drastically change the time.
Understanding how duplicate checks scale helps you explain data cleaning steps clearly and shows you know how data size affects performance.
"What if we checked duplicates on all columns instead of specific ones? How would the time complexity change?"
Practice
subset parameter do in the duplicated() method in pandas?Solution
Step 1: Understand the purpose of
Theduplicated()duplicated()method identifies duplicate rows in a DataFrame.Step 2: Role of
Thesubsetparametersubsetparameter limits the duplicate check to specific columns instead of all columns.Final Answer:
It specifies which columns to check for duplicates. -> Option AQuick Check:
subset= columns to check duplicates [OK]
- Thinking subset removes duplicates
- Confusing subset with sorting
- Assuming subset changes data types
df?Solution
Step 1: Recall pandas method for duplicates
The correct method isduplicated()to find duplicate rows.Step 2: Correct use of subset parameter
Thesubsetparameter takes a list of column names to check duplicates on.Final Answer:
df.duplicated(subset=['A', 'B']) -> Option AQuick Check:
Correct method and subset syntax [OK]
- Using non-existent methods like duplicates()
- Passing columns instead of subset
- Wrong method names like find_duplicates()
import pandas as pd
data = {'A': [1, 2, 1, 3], 'B': [4, 5, 4, 6], 'C': [7, 8, 9, 10]}
df = pd.DataFrame(data)
print(df.duplicated(subset=['A', 'B']))What is the output?
Solution
Step 1: Understand the DataFrame and subset
The DataFrame has rows with columns A and B: (1,4), (2,5), (1,4), (3,6).Step 2: Identify duplicates on columns 'A' and 'B'
The third row (index 2) duplicates the first row (index 0) on columns A and B.Final Answer:
[False, False, True, False] -> Option CQuick Check:
Only third row is duplicate on A and B [OK]
- Assuming duplicates on all columns
- Marking first occurrence as duplicate
- Confusing index positions
df.duplicated(subset=['A', 'B'], keep='last')
but get unexpected results. What is a common mistake causing this?
Solution
Step 1: Understand
keepparameter behaviorkeep='last'marks all duplicates except the last occurrence as True.Step 2: Identify common confusion
Many expect it to mark the last duplicate as True, but it actually keeps the last as False (not duplicate).Final Answer:
Using keep='last' marks the last occurrence as False, others as True. -> Option BQuick Check:
keep='last' keeps last duplicate False [OK]
- Thinking keep='last' marks last duplicate True
- Passing subset as string instead of list
- Believing duplicated() lacks keep parameter
df with columns 'Name', 'Age', 'City'. You want to remove rows where 'Name' and 'City' are duplicates but keep the first occurrence. Which code correctly does this?Solution
Step 1: Identify method to remove duplicates
Usedrop_duplicates()to remove duplicate rows.Step 2: Use subset and keep parameters correctly
Passsubset=['Name', 'City']andkeep='first'to keep first occurrence and remove others.Step 3: Check options
df = df.drop_duplicates(subset=['Name', 'City'], keep='first') uses correct method and parameters. df[~df.duplicated(subset=['Name', 'City'], keep='first')] usesduplicated()with negation but does not assign back to df. df = df.drop_duplicates(columns=['Name', 'City'], keep='last') uses wrong parameter name. df = df.duplicated(subset=['Name', 'City'], keep='first') returns boolean Series, not DataFrame.Final Answer:
df = df.drop_duplicates(subset=['Name', 'City'], keep='first') -> Option DQuick Check:
drop_duplicates with subset and keep='first' removes duplicates [OK]
- Using duplicated() instead of drop_duplicates() to remove rows
- Passing columns instead of subset
- Not assigning result back to df
Ready to prove it?
Pass the test and this pattern moves unknown
