Back to Python Notes
Topic #264

Removing Duplicates

Learn how to remove duplicate rows from a Pandas DataFrame using drop_duplicates(), ensuring your dataset contains only unique records based on specific columns or the entire row.

What it is

Duplicate removal is the process of identifying and deleting redundant entries in a dataset. In Python's Pandas library, this is handled by the DataFrame.drop_duplicates() method. A "duplicate" can be defined as an exact match across all columns or a match within a subset of key columns (e.g., removing users with the same email address but keeping different purchase dates).

Mental Model: Think of it as filtering a list where you keep the first occurrence of a value and discard subsequent identical values. The related term unique() returns distinct values from a Series, while duplicated() returns a boolean mask indicating which rows are duplicates.

Why it matters

  • Data Integrity: Prevents skewed analysis caused by double-counting events or entities.
  • Performance: Reduces memory usage and speeds up operations like grouping and joining.
  • Accuracy: Ensures metrics like averages or counts reflect true unique occurrences.
  • Cleanliness: Removes artifacts from data merging or scraping processes that often introduce redundancy.

Syntax or steps

The basic syntax is df.drop_duplicates(subset=None, keep='first', inplace=False).

  1. subset: Specify column names to check for duplicates. If None, all columns are considered.
  2. keep: Determines which duplicate to retain: 'first' (default), 'last', or False (remove all duplicates).
  3. inplace: If True, modifies the original DataFrame; otherwise, returns a new one.

Example

import pandas as pd

# Create sample data with duplicates
data = {
    'Name': ['Alice', 'Bob', 'Alice', 'Charlie', 'Bob'],
    'Age': [25, 30, 25, 35, 31],
    'City': ['NYC', 'LA', 'NYC', 'Chicago', 'LA']
}
df = pd.DataFrame(data)

print("Original DataFrame:")
print(df)

# Remove exact duplicates across all columns
df_clean = df.drop_duplicates()

print("\nAfter removing exact duplicates:")
print(df_clean)

# Remove duplicates based only on 'Name' (keeping the first occurrence)
df_unique_names = df.drop_duplicates(subset=['Name'])

print("\nAfter removing duplicates based on 'Name':")
print(df_unique_names)

Explanation: The first call removes rows where all values match previous rows. The second call uses subset=['Name'], so if two rows have the same name, only the first one encountered is kept, regardless of age or city differences.

Common mistakes

  • Ignoring Order: By default, keep='first' retains the earliest row. If your data is unsorted, you might keep outdated information. Sort the DataFrame before dropping duplicates if necessary.
  • Forgetting Assignment: df.drop_duplicates() does not modify df unless inplace=True. Always assign the result: df = df.drop_duplicates().
  • Case Sensitivity: "Alice" and "alice" are treated as different. Use str.lower() on relevant columns before deduplication if case-insensitive matching is required.
  • NaN Handling: Rows with NaN in the subset columns may behave unexpectedly depending on version. Ensure critical keys are non-null or handle them explicitly.

When to use it

Compare drop_duplicates() with groupby().first():

MethodBest ForBehavior
drop_duplicates()Simple removal of redundant rowsKeeps one full row per unique key combination.
groupby().agg()Aggregating data after deduplicationCombines multiple rows into one summary row (e.g., summing sales).

Use drop_duplicates() when you want to clean raw data. Use groupby() when you need to consolidate information from duplicates into a single record.

Practice

Guided Exercise: Create a DataFrame with columns ID and Status. Add three rows with ID 1 (Status: Active, Pending, Active). Use drop_duplicates(subset=['ID'], keep='last') to see which status remains.

Challenge: How would you remove duplicates based on Email but keep the row with the most recent Login_Date? Hint: Sort by date descending before dropping duplicates.

Quick check

Q: What happens if you set keep=False in drop_duplicates()?
A: All duplicate rows are removed, including the first occurrence. Only completely unique rows remain.

Summary

Pandas drop_duplicates() is essential for cleaning datasets by removing redundant entries based on full-row matches or specific key columns. Mastering the subset and keep parameters allows precise control over which records survive, ensuring accurate downstream analysis.

Want to go beyond the notes?

Join Coding Now Tech Institute's Python course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

Removing Duplicates – FAQs

Quick answers about learning Removing Duplicates in Python.

This free note from Coding Now Tech Institute explains Removing Duplicates in Python — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Python topic on Coding Now Tech Institute, including Removing Duplicates, is 100% free with no signup required.
With focused practice, most students grasp Removing Duplicates in 1–3 days from these notes; pairing it with Coding Now Tech Institute's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the Coding Now Tech Institute Community (/community) — expert instructors answer within 24 hours.
Call NowEnroll Now