Skip to content

DOC: Multi-conditional examples added to .loc docstring #53572

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 7 commits into from
Jun 12, 2023
23 changes: 23 additions & 0 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,29 @@ def loc(self) -> _LocIndexer:
max_speed
sidewinder 7

Multiple conditional using ``&`` that returns a boolean Series

>>> df.loc[(df['max_speed'] > 1) & (df['shield'] < 8)]
max_speed shield
viper 4 5

Multiple conditional using ``|`` that returns a boolean Series

>>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)]
max_speed shield
cobra 1 2
sidewinder 7 8

Please ensure that each condition is wrapped in parentheses ``()``.
See the :ref:`user guide<indexing.boolean>`
for more details and explanations of Boolean indexing.

.. note::
If you find yourself using 3 or more conditionals in ``.loc[]``,
consider using :ref:`advanced indexing<advanced.advanced_hierarchical>`.

See below for using ``.loc[]`` on MultiIndex DataFrames.

Callable that returns a boolean Series

>>> df.loc[lambda df: df['shield'] == 8]
Expand Down