Editor

Remove Duplicates

Intermediate · Write the code · from Build an Automated Data Cleaning Pipeline

From the lesson: Step 3: Remove Duplicate Rows

Duplicate rows inflate your analysis. If Alice appears twice, her purchase total gets counted twice. Pandas makes deduplication easy with drop_duplicates(). You can choose which columns determine whether two rows are "the same."

Read this part of the lesson

Write a function remove_duplicates(df) that:

1. Prints how many duplicate rows were found

2. Removes duplicates based on the name and email columns (keep the first occurrence)

3. Resets the index (with drop=True)

4. Returns the cleaned DataFrame

Print format: Duplicates found: X

import pandas as pd
def remove_duplicates(df):
# Your code here
pass
data = {
'name': ['Alice', 'Bob', 'Alice', 'Charlie', 'Bob'],
'email': ['a@t.com', 'b@t.com', 'a@t.com', 'c@t.com', 'b@t.com'],
'score': [100, 200, 100, 150, 200]
}
df = pd.DataFrame(data)
cleaned = remove_duplicates(df)
print(f'Rows remaining: {len(cleaned)}')

More Data Cleaning Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz