Skip to content

Commit d7af4db

Browse files
authored
DOC: Multi-conditional examples added to .loc docstring (pandas-dev#53572)
* Add multi-conditional examples in loc docstring * Update boolean operators in example descriptions * Update note on advanced indexing * Add point to wrap conditionals in parens.
1 parent 94a8af5 commit d7af4db

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

pandas/core/indexing.py

+23
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,29 @@ def loc(self) -> _LocIndexer:
400400
max_speed
401401
sidewinder 7
402402
403+
Multiple conditional using ``&`` that returns a boolean Series
404+
405+
>>> df.loc[(df['max_speed'] > 1) & (df['shield'] < 8)]
406+
max_speed shield
407+
viper 4 5
408+
409+
Multiple conditional using ``|`` that returns a boolean Series
410+
411+
>>> df.loc[(df['max_speed'] > 4) | (df['shield'] < 5)]
412+
max_speed shield
413+
cobra 1 2
414+
sidewinder 7 8
415+
416+
Please ensure that each condition is wrapped in parentheses ``()``.
417+
See the :ref:`user guide<indexing.boolean>`
418+
for more details and explanations of Boolean indexing.
419+
420+
.. note::
421+
If you find yourself using 3 or more conditionals in ``.loc[]``,
422+
consider using :ref:`advanced indexing<advanced.advanced_hierarchical>`.
423+
424+
See below for using ``.loc[]`` on MultiIndex DataFrames.
425+
403426
Callable that returns a boolean Series
404427
405428
>>> df.loc[lambda df: df['shield'] == 8]

0 commit comments

Comments
 (0)