Identifying Duplicates

Python · Data Cleaning

Theory

df.duplicated() returns a boolean Series that is True for every row that repeats an earlier row. By default it compares all columns; subset=['name', 'city'] compares only those columns, which is how you find duplicate business keys even when other columns differ. The keep argument decides which copy is not flagged: keep='first' (default) flags all copies except the first, keep='last' flags all except the last, and keep=False flags every row that has a twin.

So df.duplicated().sum() tells you how many extra copies exist, and df[df.duplicated(subset=['name', 'city'], keep=False)] shows every row involved, sorted for review. To see how many times each key appears, use df.value_counts(['name', 'city']) or df.groupby(['name', 'city']).size() and keep counts above 1 - the pandas version of SQL's GROUP BY ... HAVING COUNT(*) > 1. Text keys often hide duplicates behind case or spaces, so normalise them with .str.strip().str.lower() first.

Why we use it

Duplicates inflate totals, double-count customers and make joins explode into extra rows. Finding them, and understanding why they exist, comes before deciding which copy to keep.

When to use it

Check for duplicates after loading, after concatenating sources and before any join on a key. Use keep=False to review every copy; use subset when only the key columns matter. To actually remove them, use drop_duplicates.

How it works

  1. df.duplicated(subset=key, keep=...)
  2. Build each row's key from the subset columns
  3. Has this key appeared before? Yes: Candidate duplicate. No: First copy - not flagged.
  4. keep='last' or False? Yes: Also flag earlier copies. No: Flag only later copies.
  5. Boolean Series: True = duplicate

Syntax

df.duplicated(subset=None, keep='first' | 'last' | False)
df.duplicated().sum()
df[df.duplicated(subset=[...], keep=False)]

Example

print(df.duplicated(subset=['name', 'city']).sum())
print(df[df.duplicated(subset=['name', 'city'], keep=False)])

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic