PY-401 ยท Cleaning messy data
Finding and removing duplicates
โฑ 9 min ๐ Real Python 3 ๐ฌ Animated โ Completed
โก The 30-second answer
Duplicates are rows that appear more than once, often from exporting the same data twice or a form submitted twice. df.duplicated() marks every repeat as True, and df.drop_duplicates() removes them. Use subset=["email"] to compare only some columns, and keep="last" to keep the newest copy instead of the first.
- Spot repeated rows with duplicated
- Remove them with drop_duplicates
- Choose which copy to keep with subset and keep
Try it
Watch it come alive, run it yourself, then prove it. Real Python, right on this page.
Watch Python run the example. Every variable is a jar; watch the values change and the output appear.
import pandas as pdsignups = pd.DataFrame({"email": ["asha@x.com", "ben@x.com", "asha@x.com", "chen@x.com", "ben@x.com"],"plan": ["free", "pro", "free", "free", "team"],})print(signups.duplicated().tolist())print(signups.drop_duplicates())latest = signups.drop_duplicates(subset="email", keep="last")print(latest)
Change anything and press Run (or Ctrl+Enter). Then open Watch it and press Watch my code.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Expected output
[False, False, True, False, False]
email plan
0 asha@x.com free
1 ben@x.com pro
3 chen@x.com free
4 ben@x.com team
email plan
2 asha@x.com free
3 chen@x.com free
4 ben@x.com team ๐ฏ Set unique_customers to orders with one row per customer, keeping each customer's first order. Then print how many rows it has.
Input one line per input() call
Press Run to see the output. Python runs in your browser. Need a hint?
unique_customers = orders.drop_duplicates(subset="customer") then print(len(unique_customers))
- The problemMarketing says 5 people signed up this week. Asha clicked the button twice and Ben upgraded his plan later, so the real number of people is 3.
- The Python idea
df.duplicated()Duplicates are rows that appear more than once, often from exporting the same data twice or a form submitted twice. - In real lifeA CRM merges contacts from a web form and a trade-show scanner.
drop_duplicates(subset=["email"], keep="last")keeps one record per person with their latest details.
Start with a question
Marketing says 5 people signed up this week. Asha clicked the button twice and Ben upgraded his plan later, so the real number of people is 3.
Duplicates inflate counts and totals: a customer counted twice, an order charged twice in a report. Removing them is a standard cleaning step.
Explain it like I'm 10
You have a pile of name cards and some names appear twice. Removing duplicates is going through the pile and keeping only one card for each name.
The technical explanation
df.duplicated() is True for each row that exactly matches an earlier row in every column.
df.drop_duplicates() keeps the first copy of each and removes the rest. The kept rows keep their index labels.
subset=["email"] compares only that column, so two rows with the same email count as duplicates even if their plan differs.
keep="last" keeps the latest copy, which is usually the most up to date. keep=False drops every copy, including the first.
Syntax
df.duplicated()
df.drop_duplicates()
df.drop_duplicates(subset=["col"], keep="last") Real-world scenario
A CRM merges contacts from a web form and a trade-show scanner. drop_duplicates(subset=["email"], keep="last") keeps one record per person with their latest details.
Test yourself
5 questions. Get 4 right and this lesson is marked complete.
Pass the quiz to complete this lesson.
๐ Every lesson counts toward your certificate Next: Cleaning text columns โQuestions people ask
How do I remove duplicates based on one column in pandas?
Use df.drop_duplicates(subset=["column"]). Add keep="last" to keep the most recent row for each value instead of the first.
How do I see the duplicate rows before removing them?
Filter with the mask: df[df.duplicated(keep=False)] shows every row that has a twin, including the first copy, so you can check them before deleting anything.
Interview questions
How this topic comes up in Python interviews. Answer out loud first, then open the model answer.
beginner How do I remove duplicates based on one column in pandas?
Use df.drop_duplicates(subset=["column"]). Add keep="last" to keep the most recent row for each value instead of the first.
beginner How do I see the duplicate rows before removing them?
Filter with the mask: df[df.duplicated(keep=False)] shows every row that has a twin, including the first copy, so you can check them before deleting anything.
intermediate What does df.duplicated() return?
True/False per row, True for repeats. It marks each row that repeats an earlier one.
intermediate Which copy does drop_duplicates() keep by default?
The first. keep="first" is the default.
Quick revision
Remember
- Spot repeated rows with duplicated
- Remove them with drop_duplicates
- Choose which copy to keep with subset and keep
- Count before and after:
len(df)thenlen(df.drop_duplicates()). If half your rows vanish, your subset is probably too narrow.
Syntax
df.duplicated()
df.drop_duplicates()
df.drop_duplicates(subset=["col"], keep="last") Official documentation
- pandas.DataFrame.drop_duplicates (docs.python.org)