Lesson 13 of 23
โฎPrev Nextโฏ

PY-401 ยท Cleaning messy data

Finding and removing duplicates

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

13of 23

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก 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.

    Finding and removing duplicates, running
    1. import pandas as pd
    2. signups = pd.DataFrame({
    3. "email": ["asha@x.com", "ben@x.com", "asha@x.com", "chen@x.com", "ben@x.com"],
    4. "plan": ["free", "pro", "free", "free", "team"],
    5. })
    6. print(signups.duplicated().tolist())
    7. print(signups.drop_duplicates())
    8. latest = signups.drop_duplicates(subset="email", keep="last")
    9. print(latest)
    Press โ–ถ to watch Python run this, one line at a time.
    1. 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.
    2. The Python ideadf.duplicated()Duplicates are rows that appear more than once, often from exporting the same data twice or a form submitted twice.
    3. 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")

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    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.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does df.duplicated() return?
    2. 2. Which copy does drop_duplicates() keep by default?
    3. 3. How do you treat rows with the same email as duplicates?
    4. 4. What does keep=False do?
    5. 5. How do you count duplicate rows?

    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) then len(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