Coding Now – Best AI & Full Stack Courses in Delhi NCR | 100% Placement
Limited Offer: Get 50% OFF on AI & Full Stack Courses
📞 Call Now: +91 9667708830
Back to Machine Learning Notes
Topic #405

Duplicate Data

Duplicate rows silently give some data points extra "votes" during training — a model trained on a dataset with repeated rows effectively over-weights whatever those rows represent, without you asking it to.

Detecting and Removing Duplicates

import pandas as pd

df = pd.read_csv("transactions.csv")

print(df.duplicated().sum())              # count of exact duplicate rows
df_clean = df.drop_duplicates()             # remove exact duplicates, keep first occurrence

# Duplicates based on a subset of columns (e.g. same customer, same day, same amount)
print(df.duplicated(subset=["customer_id", "date", "amount"]).sum())
df_clean = df.drop_duplicates(subset=["customer_id", "date", "amount"], keep="first")

Exact Duplicates vs Near-Duplicates

TypeExampleDetection Approach
Exact duplicateIdentical row appears twice — often from a data pipeline bug (double-submitted form, re-import)df.duplicated()
Near-duplicateSame customer logged with slightly different spelling/formattingFuzzy matching, or normalize text first (see Data Cleaning) then re-check exact duplicates

Why This Isn't Always "Just Remove Them"

Not every repeated row is a bug — a customer genuinely making the same $10 purchase twice in one day is two legitimate transactions, not a duplicate. Always check why rows match before deleting: exact duplicates across every column are usually safe to drop; duplicates only on a business key (like transaction_id) are almost always a pipeline error.

Common Mistakes

  • Dropping duplicates before splitting into train/test — if a duplicate row lands in both sets, the model effectively "sees" a test example during training, inflating evaluation metrics.
  • Using drop_duplicates() with no subset when the real business duplicate key is only a few columns (like an ID), missing near-duplicates that differ only in an irrelevant timestamp column.

Interview Relevance

Q: "Why can duplicate rows be a problem even for otherwise clean data?" They silently bias the model toward whatever pattern the duplicated rows represent, and if duplicates span the train/test split, they inflate test performance in a way that won't hold on genuinely new data.

Practice Question

A transactions dataset has duplicate rows on (customer_id, date, amount) but different transaction_id values. Are these safe to drop as duplicates? What would you check first?

Related ML Notes

Want to go beyond the notes?

Join CodingNow's Machine Learning course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available
💬 Talk to Advisor
1
WhatsApp

Latest from Our Blog

Insights on AI, Data Science, Full Stack & Career

View All Articles →