🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to Machine Learning Notes
Topic #56

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?

Want to go beyond the notes?

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

Enroll Now — Free Demo Available

Duplicate Data – FAQs

Quick answers about learning Duplicate Data in Machine Learning.

This free note from CodingNow 2.0 explains Duplicate Data in Machine Learning — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Machine Learning topic on CodingNow 2.0, including Duplicate Data, is 100% free with no signup required.
With focused practice, most students grasp Duplicate Data in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now