Skip to content

Commit fc57bfb

Browse files
ENH: Update isin docs with examples of ~ operator usage (#43959) (#43961)
1 parent 14b4c07 commit fc57bfb

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

doc/source/user_guide/indexing.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,15 @@ a list of items you want to check for.
997997
998998
df.isin(values)
999999
1000+
To return the DataFrame of booleans where the values are *not* in the original DataFrame,
1001+
use the ``~`` operator:
1002+
1003+
.. ipython:: python
1004+
1005+
values = {'ids': ['a', 'b'], 'vals': [1, 3]}
1006+
1007+
~df.isin(values)
1008+
10001009
Combine DataFrame's ``isin`` with the ``any()`` and ``all()`` methods to
10011010
quickly select subsets of your data that meet a given criteria.
10021011
To select a row where each column meets its own criterion:

pandas/core/frame.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10559,6 +10559,13 @@ def isin(self, values) -> DataFrame:
1055910559
falcon True True
1056010560
dog False True
1056110561
10562+
To check if ``values`` is *not* in the DataFrame, use the ``~`` operator:
10563+
10564+
>>> ~df.isin([0, 2])
10565+
num_legs num_wings
10566+
falcon False False
10567+
dog True False
10568+
1056210569
When ``values`` is a dict, we can pass values to check for each
1056310570
column separately:
1056410571

pandas/core/series.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5012,6 +5012,17 @@ def isin(self, values) -> Series:
50125012
5 False
50135013
Name: animal, dtype: bool
50145014
5015+
To invert the boolean values, use the ``~`` operator:
5016+
5017+
>>> ~s.isin(['cow', 'lama'])
5018+
0 False
5019+
1 False
5020+
2 False
5021+
3 True
5022+
4 False
5023+
5 True
5024+
Name: animal, dtype: bool
5025+
50155026
Passing a single string as ``s.isin('lama')`` will raise an error. Use
50165027
a list of one element instead:
50175028

0 commit comments

Comments
 (0)