How to Find and Remove Duplicate Rows in a Dataset
To remove duplicate rows, first standardise the columns that identify a record (trim whitespace, normalise case, map spelling variants), then drop exact duplicates and check whether any duplicates span your train and test sets. TensorTurn flags duplicate rows and train/test leakage on upload, drops duplicates with a step you approve in its AI data editor, and finds exact and near-duplicate images, including cross-split duplicates, for image datasets.
Why duplicates matter
Duplicate rows inflate counts, double revenue, and give some records twice the weight of others in averages and models. In machine learning they do something worse: if the same row appears in both the training set and the test set, the model is graded partly on examples it has already memorised, so test accuracy looks better than real-world accuracy will be. Duplicates usually come from concatenating overlapping exports, retrying failed imports, form resubmissions and merges on non-unique keys.
Exact duplicates vs near duplicates
| Kind | Example | How to catch it |
|---|---|---|
| Exact duplicate | Two rows identical in every column | Drop duplicates directly |
| Whitespace or case variant | "Acme Ltd" vs "acme ltd " with otherwise equal values | Trim whitespace and normalize case, then drop duplicates |
| Spelling or code variant | "United States" vs "USA" | Map values to a canonical form first |
| Formatting variant | "$1,200" vs "1200" | Parse formatted numbers and cast to number first |
| Same entity, different ID | One customer created twice with two IDs | Compare on identifying columns (email, phone) rather than all columns |
| Near-duplicate image | Same photo resized, recompressed or lightly cropped | Pixel hashing plus semantic embeddings |
Most "fuzzy" duplication in tabular data disappears once the text is standardised. The order matters: normalise first, then deduplicate, otherwise a single trailing space protects a duplicate from removal.
Should you always remove duplicates?
No. Ask what one row represents. In a customer table, two identical rows are almost certainly an error. In a transaction log, two identical purchases of the same item for the same amount on the same day may be two real purchases, and removing one understates revenue. If a table has no timestamp or transaction ID, it may be impossible to tell, and it is worth checking the source system. Also consider whether duplicates carry meaning for your model: in survey data, repeated identical responses can indicate a bot.
Duplicates leaking across train and test
Train/test leakage through duplicates is one of the most common reasons a model performs brilliantly in evaluation and disappointingly in production. It happens whenever a dataset with duplicates is split randomly, or when separately supplied train and test files overlap. TensorTurn's tabular analysis includes train/test leakage detection alongside duplicate row detection, so overlap between splits is surfaced before you train. The safe pattern is to deduplicate before splitting, and when several rows belong to one entity (a patient, a customer), to split by entity so the same entity never appears on both sides.
Duplicate images
Image datasets collect duplicates from scraping, data augmentation saved to disk, and frames extracted from video. Exact byte-level checks miss resized or recompressed copies. TensorTurn checks image datasets in two passes: in-browser pixel checks catch exact and near-duplicate images as well as blurry, corrupt and badly exposed files, and a semantic GPU pass with DINOv2 embeddings finds near duplicates that look alike to a model, flags duplicates that cross the train/test split, and suggests likely-mislabeled images with a corrected label and confidence.
Step by step: remove duplicates on TensorTurn
- Upload the dataset. The profile and alerts report duplicate rows immediately.
- Decide which columns define a unique record. Delete or ignore columns such as import timestamps that make true duplicates look different.
- In the AI data editor, standardise first: "trim whitespace and lowercase the email and company columns, map USA and United States to United States".
- Parse formatted numbers with one-click auto-fix so "$1,200" and "1200" compare equal.
- Ask it to "drop duplicate rows", review the planned step and approve it.
- Re-check the profile: no duplicate rows should remain and the row count should match expectations.
- For image data, run the image checks and review flagged near duplicates and cross-split duplicates before training.
Doing it elsewhere
Excel has Remove Duplicates on the Data tab, which works on exact matches across the columns you select. pandas has drop_duplicates, with a subset argument for key columns and a keep argument to choose which copy survives. SQL users group by the key columns or use a window function to keep the first row. All of these compare exact values, so the same rule applies everywhere: standardise text before deduplicating.
Common mistakes
- Deduplicating before trimming whitespace and normalising case.
- Deduplicating on all columns when a load timestamp or row number makes every row unique.
- Removing legitimate repeat events such as two real purchases.
- Splitting into train and test before removing duplicates.
- Ignoring near-duplicate images, which leak across splits just like rows.
Frequently asked questions
How do I find duplicates in a dataset?
Profile the dataset to count exact duplicate rows, then standardise the identifying columns by trimming whitespace and normalising case to reveal near duplicates. TensorTurn reports duplicate rows automatically on upload and flags train/test leakage.
What is the difference between exact and near duplicates?
Exact duplicates match in every compared value. Near duplicates describe the same record but differ in formatting, whitespace, case or spelling, so they are only caught after standardising the text or, for images, by comparing visual similarity.
Should I remove duplicates before splitting train and test data?
Yes. If duplicates are split across train and test, the model is evaluated on rows it has already seen and the score is inflated. Deduplicate first, and split by entity when one entity has several rows.
Can TensorTurn find duplicate images?
Yes. In-browser pixel checks find exact and near-duplicate images, and a semantic GPU pass with DINOv2 finds near duplicates, cross-split leakage and likely-mislabeled images.
Does removing duplicates delete data I need?
It can if the duplicates are real repeat events, such as two identical purchases. Check what one row represents before deduplicating, and in TensorTurn you review the drop duplicates step before approving it.