Skip to content

Commit f7c7ee0

Browse files
flying-sheepjreback
authored andcommitted
Document how to drop duplicate indices
fixes #9708
1 parent 31782f4 commit f7c7ee0

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

Diff for: doc/source/indexing.rst

+10
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,16 @@ should be taken instead.
11371137
df2.drop_duplicates(['a','b'])
11381138
df2.drop_duplicates(['a','b'], take_last=True)
11391139
1140+
An easier way to drop duplicates on the index than to temporarily forgo it is
1141+
``groupby(level=0)`` combined with ``first()`` or ``last()``.
1142+
1143+
.. ipython:: python
1144+
1145+
df3 = df2.set_index('b')
1146+
df3
1147+
df3.reset_index().drop_duplicates(subset='b', take_last=False).set_index('b')
1148+
df3.groupby(level=0).first()
1149+
11401150
.. _indexing.dictionarylike:
11411151

11421152
Dictionary-like :meth:`~pandas.DataFrame.get` method

0 commit comments

Comments
 (0)