Skip to content

DOC: Updated drop_duplicates doc #10810

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 18, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 30 additions & 15 deletions doc/source/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1180,28 +1180,43 @@ takes as an argument the columns to use to identify duplicated rows.
By default, the first observed row of a duplicate set is considered unique, but
each method has a ``keep`` parameter to specify targets to be kept.

- ``keep='first'`` (default): mark / drop duplicates except for the first occurrence.
- ``keep='last'``: mark / drop duplicates except for the last occurrence.
- ``keep=False``: mark / drop all duplicates.

.. ipython:: python

df2 = pd.DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],
'b' : ['x', 'y', 'y', 'x', 'y', 'x', 'x'],
'c' : np.random.randn(7)})
df2.duplicated(['a','b'])
df2.duplicated(['a','b'], keep='last')
df2.duplicated(['a','b'], keep=False)
df2.drop_duplicates(['a','b'])
df2.drop_duplicates(['a','b'], keep='last')
df2.drop_duplicates(['a','b'], keep=False)
df2 = pd.DataFrame({'a': ['one', 'one', 'two', 'two', 'two', 'three', 'four'],
'b': ['x', 'y', 'x', 'y', 'x', 'x', 'x'],
'c': np.random.randn(7)})
df2
df2.duplicated('a')
df2.duplicated('a', keep='last')
df2.duplicated('a', keep=False)
df2.drop_duplicates('a')
df2.drop_duplicates('a', keep='last')
df2.drop_duplicates('a', keep=False)

An alternative way to drop duplicates on the index is ``.groupby(level=0)`` combined with ``first()`` or ``last()``.
Also, you can pass a list of columns to identify duplications.

.. ipython:: python

df3 = df2.set_index('b')
df3
df3.groupby(level=0).first()
df2.duplicated(['a', 'b'])
df2.drop_duplicates(['a', 'b'])

To drop duplicates by index value, use ``Index.duplicated`` then perform slicing.
Same options are available in ``keep`` parameter.

# a bit more verbose
df3.reset_index().drop_duplicates(subset='b', keep='first').set_index('b')
.. ipython:: python

df3 = pd.DataFrame({'a': np.arange(6),
'b': np.random.randn(6)},
index=['a', 'a', 'b', 'c', 'b', 'a'])
df3
df3.index.duplicated()
df3[~df3.index.duplicated()]
df3[~df3.index.duplicated(keep='last')]
df3[~df3.index.duplicated(keep=False)]

.. _indexing.dictionarylike:

Expand Down