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

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

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

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

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

Please 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[]``, you may
experience performance gains by restructuring the DataFrame into a
MultiIndex object.

See below for using ``.loc[]`` on MultiIndex DataFrames.
Please see the :ref:`user guide<advanced.advanced_hierarchical>`
for more details and explanations of advanced indexing.

Callable that returns a boolean Series

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