pandas 1.4.2

ParametersReturnsBackRef
drop_duplicates(self, subset: 'Hashable | Sequence[Hashable] | None' = None, keep: "Literal['first'] | Literal['last'] | Literal[False]" = 'first', inplace: 'bool' = False, ignore_index: 'bool' = False) -> 'DataFrame | None'

Considering certain columns is optional. Indexes, including time indexes are ignored.

Parameters

subset : column label or sequence of labels, optional

Only consider certain columns for identifying duplicates, by default use all of the columns.

keep : {'first', 'last', False}, default 'first'

Determines which duplicates (if any) to keep. - first : Drop duplicates except for the first occurrence. - last : Drop duplicates except for the last occurrence. - False : Drop all duplicates.

inplace : bool, default False

Whether to drop duplicates in place or to return a copy.

ignore_index : bool, default False

If True, the resulting axis will be labeled 0, 1, …, n - 1.

versionadded

Returns

DataFrame or None

DataFrame with duplicates removed or None if inplace=True .

Return DataFrame with duplicate rows removed.

See Also

DataFrame.value_counts

Count unique combinations of columns.

Examples

Consider dataset containing ramen rating.

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({
...  'brand': ['Yum Yum', 'Yum Yum', 'Indomie', 'Indomie', 'Indomie'],
...  'style': ['cup', 'cup', 'cup', 'pack', 'pack'],
...  'rating': [4, 4, 3.5, 15, 5]
... })
... df brand style rating 0 Yum Yum cup 4.0 1 Yum Yum cup 4.0 2 Indomie cup 3.5 3 Indomie pack 15.0 4 Indomie pack 5.0

By default, it removes duplicate rows based on all columns.

This example is valid syntax, but we were not able to check execution
>>> df.drop_duplicates()
    brand style  rating
0  Yum Yum   cup     4.0
2  Indomie   cup     3.5
3  Indomie  pack    15.0
4  Indomie  pack     5.0

To remove duplicates on specific column(s), use subset .

This example is valid syntax, but we were not able to check execution
>>> df.drop_duplicates(subset=['brand'])
    brand style  rating
0  Yum Yum   cup     4.0
2  Indomie   cup     3.5

To remove duplicates and keep last occurrences, use keep .

This example is valid syntax, but we were not able to check execution
>>> df.drop_duplicates(subset=['brand', 'style'], keep='last')
    brand style  rating
1  Yum Yum   cup     4.0
2  Indomie   cup     3.5
4  Indomie  pack     5.0
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.series.Series.drop_duplicates pandas.core.frame.DataFrame.drop pandas.core.frame.DataFrame.duplicated

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#6030
type: <class 'function'>
Commit: