Bird
Raised Fist0
Data Analysis Pythondata~15 mins

Removing duplicates (drop_duplicates) in Data Analysis Python - Deep Dive

Choose your learning style10 modes available
Overview - Removing duplicates (drop_duplicates)
What is it?
Removing duplicates means finding and deleting repeated rows in a dataset so each row is unique. In Python's pandas library, the drop_duplicates function helps do this easily. It checks rows and removes any that appear more than once, keeping only the first or last occurrence. This cleans data and prevents errors in analysis caused by repeated information.
Why it matters
Duplicates can cause wrong results in data analysis, like counting the same person twice or inflating sales numbers. Without removing duplicates, decisions based on data might be wrong, leading to wasted resources or bad strategies. Drop_duplicates solves this by quickly cleaning data, making sure each record is counted once and analysis is accurate.
Where it fits
Before learning drop_duplicates, you should know how to work with pandas DataFrames and basic data selection. After mastering duplicates removal, you can learn about data cleaning techniques like handling missing values and data transformation. This fits early in the data cleaning and preparation phase of a data science project.
Mental Model
Core Idea
Removing duplicates means keeping only one copy of repeated rows so each record is unique in the dataset.
Think of it like...
Imagine you have a stack of birthday cards, but some cards are exact copies. Removing duplicates is like keeping only one card from each set of identical cards so your collection has no repeats.
┌───────────────┐
│ Original Data │
├───────────────┤
│ Row 1: Alice  │
│ Row 2: Bob    │
│ Row 3: Alice  │  <-- duplicate
│ Row 4: Carol  │
│ Row 5: Bob    │  <-- duplicate
└───────────────┘
        ↓ drop_duplicates
┌───────────────┐
│ Cleaned Data  │
├───────────────┤
│ Row 1: Alice  │
│ Row 2: Bob    │
│ Row 4: Carol  │
└───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding duplicates in data
🤔
Concept: What duplicates are and why they appear in datasets.
Duplicates are rows that have exactly the same values in all columns or in selected columns. They can appear due to errors in data entry, merging datasets, or repeated measurements. Identifying duplicates is the first step before removing them.
Result
You can spot repeated rows that might cause errors in analysis.
Understanding what duplicates are helps you know why cleaning them is necessary to avoid misleading results.
2
FoundationIntroduction to pandas DataFrame basics
🤔
Concept: How to create and view data in pandas DataFrames.
A DataFrame is like a table with rows and columns. You can create one from a dictionary or list, and view it using print or display commands. This is the basic structure where duplicates exist.
Result
You can create and see your data in a structured form ready for cleaning.
Knowing DataFrames is essential because drop_duplicates works on this data structure.
3
IntermediateUsing drop_duplicates to remove repeated rows
🤔Before reading on: do you think drop_duplicates removes all duplicates by default or only some? Commit to your answer.
Concept: How to apply drop_duplicates to remove repeated rows and keep the first occurrence.
The drop_duplicates() function removes duplicate rows from a DataFrame. By default, it keeps the first occurrence and drops later repeats. You can call it simply as df.drop_duplicates() to get a cleaned DataFrame.
Result
The DataFrame now has only unique rows, duplicates removed.
Knowing the default behavior of drop_duplicates helps avoid accidentally losing important data.
4
IntermediateControlling which duplicates to keep
🤔Before reading on: do you think you can keep the last duplicate instead of the first? Commit to your answer.
Concept: Using the 'keep' parameter to decide which duplicate to keep: first, last, or none.
drop_duplicates has a 'keep' option: 'first' keeps the first duplicate, 'last' keeps the last, and False drops all duplicates. For example, df.drop_duplicates(keep='last') keeps the last occurrence of each duplicate.
Result
You control which duplicate row remains in the cleaned data.
Understanding 'keep' lets you tailor cleaning to your data's story, preserving the most relevant record.
5
IntermediateRemoving duplicates based on specific columns
🤔Before reading on: do you think drop_duplicates can remove duplicates by looking at only some columns? Commit to your answer.
Concept: Using the 'subset' parameter to check duplicates only on selected columns.
Sometimes duplicates matter only on certain columns. Using subset=['col1', 'col2'] tells drop_duplicates to consider only those columns when finding duplicates. Rows with same values in those columns are duplicates regardless of other columns.
Result
Duplicates are removed based on important columns, not the whole row.
Knowing how to focus on key columns prevents removing rows that differ in other important data.
6
AdvancedIn-place duplicate removal and performance
🤔Before reading on: do you think drop_duplicates changes the original DataFrame by default? Commit to your answer.
Concept: Using 'inplace=True' to modify the original DataFrame without creating a copy, and understanding performance implications.
By default, drop_duplicates returns a new DataFrame and leaves the original unchanged. Using inplace=True modifies the original DataFrame directly, saving memory. This is useful for large datasets but requires care to avoid losing data unintentionally.
Result
The original DataFrame is cleaned without extra memory use.
Knowing when to use inplace helps manage memory and data flow in real projects.
7
ExpertHandling duplicates in large-scale data pipelines
🤔Before reading on: do you think drop_duplicates always works efficiently on very large datasets? Commit to your answer.
Concept: Challenges and strategies for removing duplicates in big data, including chunk processing and indexing.
For very large datasets, drop_duplicates can be slow or memory-heavy. Experts use chunking to process data in parts or create indexes on columns to speed up duplicate detection. Sometimes, database tools or distributed computing frameworks handle duplicates better.
Result
Duplicates are removed efficiently even in huge datasets without crashing memory.
Understanding limitations of drop_duplicates guides you to scalable solutions in real-world big data projects.
Under the Hood
drop_duplicates works by scanning rows and comparing values to find repeats. Internally, it uses hashing or sorting to detect duplicates quickly. When subset columns are specified, it hashes only those columns. The function then marks duplicates and removes them based on the 'keep' parameter. If inplace=True, it modifies the original data structure in memory; otherwise, it creates a new DataFrame.
Why designed this way?
drop_duplicates was designed to be simple and flexible for common data cleaning needs. Hashing and sorting are efficient ways to detect duplicates. The 'keep' and 'subset' options provide control without complicating the interface. Inplace modification was added for memory efficiency. Alternatives like manual loops were too slow and error-prone.
┌───────────────┐
│ Input Data    │
├───────────────┤
│ Rows with     │
│ possible      │
│ duplicates    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Hashing/Sort  │
│ Rows or       │
│ Subset cols   │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Mark duplicates│
│ based on 'keep'│
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Remove marked │
│ duplicates    │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Output Data   │
│ (cleaned)     │
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does drop_duplicates remove duplicates from the original DataFrame by default? Commit to yes or no.
Common Belief:drop_duplicates changes the original DataFrame automatically.
Tap to reveal reality
Reality:By default, drop_duplicates returns a new DataFrame and leaves the original unchanged unless inplace=True is set.
Why it matters:Without knowing this, you might think your data is cleaned when it is not, leading to errors later.
Quick: Does drop_duplicates consider all columns when removing duplicates if you specify subset columns? Commit to yes or no.
Common Belief:drop_duplicates always checks all columns for duplicates regardless of subset.
Tap to reveal reality
Reality:When subset is specified, only those columns are checked for duplicates; other columns are ignored.
Why it matters:Misunderstanding this can cause unexpected rows to be removed or kept, corrupting data integrity.
Quick: If you set keep=False, does drop_duplicates keep any duplicates? Commit to yes or no.
Common Belief:keep=False keeps one occurrence of each duplicate.
Tap to reveal reality
Reality:keep=False removes all duplicates, leaving only unique rows that appear once.
Why it matters:Using keep=False without understanding can delete more data than intended.
Quick: Is drop_duplicates always efficient on very large datasets? Commit to yes or no.
Common Belief:drop_duplicates is fast and efficient no matter dataset size.
Tap to reveal reality
Reality:On very large datasets, drop_duplicates can be slow or use too much memory; special strategies are needed.
Why it matters:Ignoring performance limits can cause crashes or long delays in data pipelines.
Expert Zone
1
drop_duplicates uses hashing internally, so columns with unhashable types (like lists) can cause errors or unexpected behavior.
2
When using subset with inplace=True, the original DataFrame is modified, which can affect other references to the same data in your code.
3
drop_duplicates does not reset the index by default, so after removal, the DataFrame may have non-sequential indices that can confuse some operations.
When NOT to use
drop_duplicates is not suitable when you need fuzzy matching or approximate duplicate detection; in those cases, use specialized libraries like fuzzywuzzy or record linkage tools. Also, for streaming or very large data, consider database deduplication or distributed frameworks like Spark.
Production Patterns
In production, drop_duplicates is often combined with data validation steps and automated pipelines. It is used after merging datasets to clean overlaps, or before aggregations to ensure unique keys. Experts also log how many duplicates were removed for auditing.
Connections
Data Cleaning
drop_duplicates is a core technique within data cleaning.
Mastering duplicates removal is foundational to preparing data for any analysis or machine learning.
Hashing Algorithms
drop_duplicates uses hashing internally to detect duplicates efficiently.
Understanding hashing helps grasp why duplicate detection is fast and how data types affect it.
Database Unique Constraints
drop_duplicates mimics the effect of unique constraints in databases by ensuring row uniqueness.
Knowing database constraints clarifies why duplicates cause problems and how to prevent them at data storage level.
Common Pitfalls
#1Assuming drop_duplicates modifies the original DataFrame without inplace=True.
Wrong approach:df.drop_duplicates() print(df) # expecting duplicates removed
Correct approach:df.drop_duplicates(inplace=True) print(df) # duplicates removed in original
Root cause:Not knowing drop_duplicates returns a new DataFrame by default, so original stays unchanged.
#2Removing duplicates without specifying subset when only some columns matter.
Wrong approach:df.drop_duplicates() # removes duplicates based on all columns
Correct approach:df.drop_duplicates(subset=['important_col1', 'important_col2'])
Root cause:Misunderstanding that duplicates can be defined on specific columns, not always entire rows.
#3Using keep=False expecting to keep one duplicate.
Wrong approach:df.drop_duplicates(keep=False) # expecting one copy kept
Correct approach:df.drop_duplicates(keep='first') # keeps first duplicate
Root cause:Misinterpreting the keep parameter's effect on which duplicates remain.
Key Takeaways
Removing duplicates ensures each row in your data is unique, preventing errors in analysis.
pandas drop_duplicates is a simple, flexible tool to clean duplicates by row or selected columns.
By default, drop_duplicates keeps the first occurrence and returns a new DataFrame unless inplace=True is set.
Understanding parameters like keep and subset lets you control which duplicates remain and how they are detected.
For very large datasets, drop_duplicates may need special handling to avoid performance issues.

Practice

(1/5)
1. What does the drop_duplicates() function do in a pandas DataFrame?
easy
A. Fills missing values with zeros
B. Removes repeated rows based on all columns by default
C. Sorts the DataFrame rows in ascending order
D. Changes the data type of columns

Solution

  1. Step 1: Understand the function purpose

    drop_duplicates() is used to remove repeated rows in a DataFrame.
  2. Step 2: Default behavior

    By default, it checks all columns to find duplicates and keeps the first occurrence.
  3. Final Answer:

    Removes repeated rows based on all columns by default -> Option B
  4. Quick Check:

    drop_duplicates() removes repeated rows [OK]
Hint: Remember: drop_duplicates removes repeated rows [OK]
Common Mistakes:
  • Thinking it sorts data
  • Confusing with fillna()
  • Assuming it changes data types
2. Which of the following is the correct syntax to remove duplicate rows from a DataFrame df?
easy
A. df.drop_duplicates[]
B. df.remove_duplicates()
C. df.drop_duplicates()
D. df.drop_duplicates{}

Solution

  1. Step 1: Identify correct method call

    The method to remove duplicates is drop_duplicates(), called with parentheses.
  2. Step 2: Check syntax correctness

    Only df.drop_duplicates() is valid syntax; brackets or braces are incorrect.
  3. Final Answer:

    df.drop_duplicates() -> Option C
  4. Quick Check:

    Use parentheses to call methods [OK]
Hint: Use parentheses () to call drop_duplicates method [OK]
Common Mistakes:
  • Using square brackets instead of parentheses
  • Using a non-existent method name
  • Using curly braces instead of parentheses
3. Given the DataFrame df below, what will df.drop_duplicates(subset=['Name']) return?
import pandas as pd
data = {'Name': ['Anna', 'Bob', 'Anna', 'Cody'], 'Age': [25, 30, 25, 22]}
df = pd.DataFrame(data)
medium
A. [{'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Cody', 'Age': 22}]
B. [{'Name': 'Anna', 'Age': 25}, {'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Cody', 'Age': 22}]
C. [{'Name': 'Bob', 'Age': 30}, {'Name': 'Cody', 'Age': 22}]
D. [{'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}]

Solution

  1. Step 1: Understand subset parameter

    Using subset=['Name'] means duplicates are checked only on the 'Name' column.
  2. Step 2: Identify duplicates by 'Name'

    'Anna' appears twice; only the first occurrence is kept, others dropped.
  3. Final Answer:

    [{'Name': 'Anna', 'Age': 25}, {'Name': 'Bob', 'Age': 30}, {'Name': 'Cody', 'Age': 22}] -> Option A
  4. Quick Check:

    Duplicates dropped by 'Name' column [OK]
Hint: Use subset to check duplicates on specific columns [OK]
Common Mistakes:
  • Not using subset and dropping all duplicates
  • Keeping all duplicates instead of first
  • Confusing which rows are dropped
4. What is wrong with this code snippet to remove duplicates from DataFrame df?
df.drop_duplicates(inplace=True)

After running it, duplicates still appear in df. Why?
medium
A. The DataFrame has no duplicates to remove
B. drop_duplicates() does not modify the original DataFrame even with inplace=True
C. The DataFrame was not reassigned and inplace=True is ignored
D. The inplace parameter is misspelled

Solution

  1. Step 1: Check inplace parameter usage

    inplace=True modifies the original DataFrame, so no reassignment needed.
  2. Step 2: Consider DataFrame content

    If duplicates still appear, likely the DataFrame had no duplicates to remove.
  3. Final Answer:

    The DataFrame has no duplicates to remove -> Option A
  4. Quick Check:

    inplace=True modifies original; no duplicates means no change [OK]
Hint: Check if DataFrame actually has duplicates before drop_duplicates [OK]
Common Mistakes:
  • Assuming inplace=True does nothing
  • Thinking drop_duplicates always changes data
  • Not verifying if duplicates exist
5. You have a DataFrame df with columns ['ID', 'Name', 'Score']. Some rows have duplicate ID values but different Score. How do you keep only the row with the highest Score for each ID?
hard
A. Use df.drop_duplicates(subset=['Score'])
B. Use df.drop_duplicates(subset=['ID']) without sorting
C. Use df.drop_duplicates(keep='last') without sorting
D. Use df.sort_values('Score', ascending=False).drop_duplicates(subset=['ID'])

Solution

  1. Step 1: Sort DataFrame by Score descending

    Sorting by 'Score' descending puts highest scores first for each ID.
  2. Step 2: Drop duplicates by ID keeping first

    After sorting, dropping duplicates by 'ID' keeps the first (highest score) row per ID.
  3. Final Answer:

    df.sort_values('Score', ascending=False).drop_duplicates(subset=['ID']) -> Option D
  4. Quick Check:

    Sort then drop duplicates keeps highest score per ID [OK]
Hint: Sort by score descending before drop_duplicates to keep max [OK]
Common Mistakes:
  • Not sorting before dropping duplicates
  • Dropping duplicates on wrong column
  • Using keep='last' without sorting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard