How do I get a list of all the duplicate items using pandas in python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Duplicate rows in a DataFrame are one of the most common data quality issues you will encounter. Whether you are cleaning survey responses, merging tables from different sources, or validating ETL output, you need a reliable way to find and inspect every duplicate. Pandas gives you several tools for this, and understanding how each one behaves will save you from silently dropping rows or missing hidden duplicates.
Using duplicated() with keep Options
The duplicated() method returns a boolean Series that marks each row as True if it is a duplicate. The keep parameter controls which occurrence is considered the "original."
Use keep=False when you want to see every row involved in a duplication, not just the "extra" copies. This is the option most people actually need during data auditing.
You can also check duplicates across specific columns only:
Using value_counts() to Spot Duplicates
Sometimes you do not need the full rows. You just want to know which values appear more than once. The value_counts() method counts occurrences and sorts by frequency:
This is a quick way to get a summary without pulling entire rows. It works well on single columns and is especially useful in exploratory analysis.
Using groupby().filter()
When you need to retrieve all rows belonging to groups that have more than one member, groupby().filter() gives you a clean one-liner:
This is functionally similar to duplicated(subset=['Name'], keep=False), but groupby().filter() is more flexible. You can apply any condition to the group, for example filtering groups where the Age values are not all the same:
Using drop_duplicates()
Once you have identified duplicates, you often want to remove them. The drop_duplicates() method mirrors the keep parameter from duplicated():
If you pass keep=False, every row that has a duplicate is dropped, leaving only rows that were unique to begin with:
Common Pitfalls
- Forgetting that
keep='first'is the default: Many developers calldf[df.duplicated()]expecting to see all duplicated rows, but this hides the first occurrence of each group. Usekeep=Falsefor a complete picture. - Checking duplicates on all columns when you only care about a key: Without the
subsetparameter,duplicated()compares every column. Two rows with the same ID but different timestamps will not be flagged. Always specifysubsetwhen your definition of "duplicate" is based on specific columns. - Mutating the DataFrame while iterating over duplicates: Dropping rows inside a loop that references the original DataFrame can cause index errors or skipped rows. Build the list of duplicates first, then drop them in a single operation.
- Ignoring case and whitespace differences:
"Alice"and" alice "are different strings to pandas. Normalize your data withstr.strip().str.lower()before checking for duplicates if case or whitespace varies. - Using
drop_duplicates()without resetting the index: After dropping rows, the index has gaps. Downstream code that relies on sequential indexing (likeilocwith a range) may break. Call.reset_index(drop=True)after deduplication.
Summary
duplicated(keep=False)is the most useful variant for auditing because it flags every row involved in a duplication, not just the extras.value_counts()gives a quick frequency table to identify which values appear more than once without pulling full rows.groupby().filter()retrieves complete rows for any group matching a custom condition, making it more flexible thanduplicated()alone.drop_duplicates()removes duplicate rows and supports the samekeepandsubsetparameters for precise control.- Always specify
subsetwhen your duplicate definition is based on key columns rather than entire rows.

