🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpandas

pandas Documentation

LOADING ENGINE...

df.drop_duplicates()

AI & DATA SCIENCE // df-drop-duplicates

df.drop_duplicates() removes rows that are exact duplicates of an earlier row, keeping the first occurrence by default.

Syntax

df.drop_duplicates(subset=None, keep='first')

Deep Dive Course

By default, drop_duplicates() compares every column when deciding whether two rows are duplicates, keeping only the first occurrence of each unique combination and dropping the rest. The subset parameter restricts the duplicate check to specific columns, so rows are considered duplicates if just those columns match, even if other columns differ; keep controls which occurrence survives — 'first', the default, 'last', or False to drop every occurrence of anything that has a duplicate at all, keeping none of them.

1Understanding df.drop_duplicates()

By default, drop_duplicates() compares every column when deciding whether two rows are duplicates, keeping only the first occurrence of each unique combination and dropping the rest. The subset parameter restricts the duplicate check to specific columns, so rows are considered duplicates if just those columns match, even if other columns differ; keep controls which occurrence survives — 'first', the default, 'last', or False to drop every occurrence of anything that has a duplicate at all, keeping none of them.

💡

Use subset=['email'] with drop_duplicates() to remove rows with a duplicate email specifically, even if other columns like name or signup date differ between those rows — the default behavior only catches rows that are duplicates across every single column.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Alice"], "age": [30, 25, 30]})
print(df.drop_duplicates())
localhost:3000

2Practical Example

Here is a real-world application of df.drop_duplicates() showing how it is used in production Pandas code.

editor.html
import pandas as pd

df = pd.DataFrame({"email": ["a@x.com", "b@x.com", "a@x.com"], "name": ["Alice", "Bob", "Alice V2"]})
print(df.drop_duplicates(subset=["email"], keep="last"))
localhost:3000

3Best Practices

Follow these guidelines when working with df.drop_duplicates():

1. Use subset to define duplicates based on the columns that actually determine uniqueness for your data, like an email or ID, instead of relying on an exact full-row match

2. Check df.duplicated().sum() before dropping, to understand how many rows would actually be removed

3. Choose keep='last' instead of the default 'first' when a later row represents more up-to-date or authoritative information than an earlier one

⚠️

Tip: Use subset=['email'] with drop_duplicates() to remove rows with a duplicate email specifically, even if other columns like name or signup date differ between those rows — the default behavior only catches rows that are duplicates across every single column.

editor.html
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Alice"], "age": [30, 25, 30]})
print(df.drop_duplicates())
localhost:3000

Examples

Example 01Basic Usage
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Alice"], "age": [30, 25, 30]})
print(df.drop_duplicates())
Example 02Advanced Example
import pandas as pd

df = pd.DataFrame({"email": ["a@x.com", "b@x.com", "a@x.com"], "name": ["Alice", "Bob", "Alice V2"]})
print(df.drop_duplicates(subset=["email"], keep="last"))

Best Practices

  • Use subset to define duplicates based on the columns that actually determine uniqueness for your data, like an email or ID, instead of relying on an exact full-row match
  • Check df.duplicated().sum() before dropping, to understand how many rows would actually be removed
  • Choose keep='last' instead of the default 'first' when a later row represents more up-to-date or authoritative information than an earlier one

Interview Question

Why might two rows that look identical when printed still not be removed by df.drop_duplicates()?

Hint: Think about columns that aren't visible or obvious just from looking at the printed output.

By default, drop_duplicates() compares every column in the DataFrame, including ones that might not be obviously visible if you're only glancing at a few key columns, like a hidden timestamp, an auto-incrementing ID, or a column with trailing whitespace or subtly different capitalization in an otherwise-matching text value. Any single differing column, even one you didn't consciously notice, is enough to make two rows count as distinct rather than duplicates, which is exactly why using subset to specify only the columns that should actually determine duplication is often necessary for the result to match what you intuitively expect.

Exercises

MediumPractice using df.drop_duplicates() in a real scenario.
View Solution
import pandas as pd

df = pd.DataFrame({"name": ["Alice", "Bob", "Alice"], "age": [30, 25, 30]})
print(df.drop_duplicates())

Frequently Asked Questions

Why might two rows that look identical when printed still not be removed by df.drop_duplicates()?

By default, drop_duplicates() compares every column in the DataFrame, including ones that might not be obviously visible if you're only glancing at a few key columns, like a hidden timestamp, an auto-incrementing ID, or a column with trailing whitespace or subtly different capitalization in an otherwise-matching text value. Any single differing column, even one you didn't consciously notice, is enough to make two rows count as distinct rather than duplicates, which is exactly why using subset to specify only the columns that should actually determine duplication is often necessary for the result to match what you intuitively expect.

Related Functions

df-dropdf-isinsets