Skip to content

Commit 94e3fd4

Browse files
committed
DOC: expand the documentation on the xs method
1 parent 626ba40 commit 94e3fd4

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

doc/source/indexing.rst

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,41 @@ selecting data at a particular level of a MultiIndex easier.
15901590
15911591
df.xs('one', level='second')
15921592
1593+
You can also select on the columns with :meth:`~pandas.MultiIndex.xs`, by
1594+
providing the axis argument
1595+
1596+
.. ipython:: python
1597+
1598+
df = df.T
1599+
df.xs('one', level='second', axis=1)
1600+
1601+
:meth:`~pandas.MultiIndex.xs` also allows selection with multiple keys
1602+
1603+
.. ipython:: python
1604+
1605+
df.xs(('one', 'bar'), level=('second', 'first'), axis=1)
1606+
1607+
1608+
.. versionadded:: 0.13.0
1609+
1610+
You can pass ``drop_level=False`` to :meth:`~pandas.MultiIndex.xs` to retain
1611+
the level that was selected
1612+
1613+
.. ipython::
1614+
1615+
df.xs('one', level='second', axis=1, drop_level=False)
1616+
1617+
versus the result with ``drop_level=True`` (the default value)
1618+
1619+
.. ipython::
1620+
1621+
df.xs('one', level='second', axis=1, drop_level=True)
1622+
1623+
.. ipython::
1624+
:suppress:
1625+
1626+
df = df.T
1627+
15931628
.. _indexing.advanced_reindex:
15941629

15951630
Advanced reindexing and alignment with hierarchical index

0 commit comments

Comments
 (0)