Skip to content

ENH: Update isin docs with examples of ~ operator usage (#43959) #43961

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 1 commit into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions doc/source/user_guide/indexing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,15 @@ a list of items you want to check for.

df.isin(values)

To return the DataFrame of booleans where the values are *not* in the original DataFrame,
use the ``~`` operator:

.. ipython:: python

values = {'ids': ['a', 'b'], 'vals': [1, 3]}

~df.isin(values)

Combine DataFrame's ``isin`` with the ``any()`` and ``all()`` methods to
quickly select subsets of your data that meet a given criteria.
To select a row where each column meets its own criterion:
Expand Down
7 changes: 7 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -10559,6 +10559,13 @@ def isin(self, values) -> DataFrame:
falcon True True
dog False True

To check if ``values`` is *not* in the DataFrame, use the ``~`` operator:

>>> ~df.isin([0, 2])
num_legs num_wings
falcon False False
dog True False

When ``values`` is a dict, we can pass values to check for each
column separately:

Expand Down
11 changes: 11 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -5012,6 +5012,17 @@ def isin(self, values) -> Series:
5 False
Name: animal, dtype: bool

To invert the boolean values, use the ``~`` operator:

>>> ~s.isin(['cow', 'lama'])
0 False
1 False
2 False
3 True
4 False
5 True
Name: animal, dtype: bool

Passing a single string as ``s.isin('lama')`` will raise an error. Use
a list of one element instead:

Expand Down