Remove exact duplicate rows
Preparing records for analysis or export
Pandas · Module
Remove duplicate rows from a DataFrame, optionally judging duplicates by selected columns. Choose whether to keep the first, last, or none of each duplicate group, and optionally reset the result index.
The method identifies repeated rows using every column unless subset selects particular columns. It returns a deduplicated DataFrame by default; keep chooses the retained occurrence, ignore_index creates a fresh sequential index, and inplace changes the original object instead of returning a DataFrame.
DataFrame.drop_duplicates(subset=None, *, keep="first", inplace=False, ignore_index=False)| Name | Type | Description |
|---|---|---|
subset | column label or iterable of labels, optional | Column label or iterable of labels used for identifying duplicates; if omitted, all columns are used. |
keep | {"first", "last", False}, default "first" | Which duplicate occurrence to retain: "first", "last", or False. |
inplace | bool, default False | If True, modify the existing DataFrame instead of returning a new one. |
ignore_index | bool, default False | If True, use a new default integer index in the result. |
DataFrame or None: a DataFrame with duplicates removed, or None when inplace=True.
import pandas as pd
df = pd.DataFrame({
"user": ["a", "a", "b"],
"score": [10, 10, 8],
})
unique = df.drop_duplicates(subset=["user"], keep="first", ignore_index=True)
print(unique)Keeps the first row for each user, considering only the user column, and rebuilds the result index.
Preparing records for analysis or export
Selecting one record per identifier
Eliminating every ambiguous repeated record
By default, duplicate detection considers all columns and retains the first occurrence. Use subset to restrict the comparison to selected columns.
Follow-up: How would you retain the latest record per key? Use subset for the key columns and keep="last", assuming the rows are already ordered by recency.
keep="first" retains the first duplicate occurrence, keep="last" retains the last, and keep=False removes every row belonging to a duplicate group.
Follow-up: What happens when keep=False is used on a group appearing three times? All three rows are removed.
The method returns a DataFrame containing duplicates removed, unless inplace=True, in which case it returns None. ignore_index=True makes the returned rows use a new default index.
Follow-up: Why can relying on the old index be misleading after filtering? Removed rows leave gaps unless the index is explicitly rebuilt.
DataFrame.duplicatedDataFrame.dropnaDataFrame.rename