Skip to content

Commit a88aa01

Browse files
author
TomAugspurger
committed
DOC: Add isin example to .13 release notes
DOC: Create isin indexing section and link whats new to it DOC: Change series.isin example.
1 parent cd1bea8 commit a88aa01

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

doc/source/indexing.rst

+33-9
Original file line numberDiff line numberDiff line change
@@ -519,21 +519,14 @@ of the DataFrame):
519519
520520
df[df['A'] > 0]
521521
522-
Consider the ``isin`` method of Series, which returns a boolean vector that is
523-
true wherever the Series elements exist in the passed list. This allows you to
524-
select rows where one or more columns have values you want:
522+
List comprehensions and ``map`` method of Series can also be used to produce
523+
more complex criteria:
525524

526525
.. ipython:: python
527526
528527
df2 = DataFrame({'a' : ['one', 'one', 'two', 'three', 'two', 'one', 'six'],
529528
'b' : ['x', 'y', 'y', 'x', 'y', 'x', 'x'],
530529
'c' : randn(7)})
531-
df2[df2['a'].isin(['one', 'two'])]
532-
533-
List comprehensions and ``map`` method of Series can also be used to produce
534-
more complex criteria:
535-
536-
.. ipython:: python
537530
538531
# only want 'two' or 'three'
539532
criterion = df2['a'].map(lambda x: x.startswith('t'))
@@ -553,6 +546,26 @@ and :ref:`Advanced Indexing <indexing.advanced>` you may select along more than
553546
554547
df2.loc[criterion & (df2['b'] == 'x'),'b':'c']
555548
549+
.. _indexing.basics.indexing_isin:
550+
551+
Indexing with isin
552+
~~~~~~~~~~~~~~~~~~
553+
554+
Consider the ``isin`` method of Series, which returns a boolean vector that is
555+
true wherever the Series elements exist in the passed list. This allows you to
556+
select rows where one or more columns have values you want:
557+
558+
.. ipython:: python
559+
560+
s = Series(np.arange(5),index=np.arange(5)[::-1],dtype='int64')
561+
562+
s
563+
564+
s.isin([2, 4])
565+
566+
s[s.isin([2, 4])]
567+
568+
556569
DataFrame also has an ``isin`` method. When calling ``isin``, pass a set of
557570
values as either an array or dict. If values is an array, ``isin`` returns
558571
a DataFrame of booleans that is the same shape as the original DataFrame, with True
@@ -585,6 +598,17 @@ You can also describe columns using integer location:
585598
586599
df.isin(values, iloc=True)
587600
601+
Combine DataFrame's ``isin`` with the ``any()`` and ``all()`` methods to
602+
quickly select subsets of your data that meet a given criteria.
603+
To select a row where each column meets its own criterion:
604+
605+
.. ipython:: python
606+
607+
values = {'ids': ['a', 'b'], 'ids2': ['a', 'c'], 'vals': [1, 3]}
608+
609+
row_mask = df.isin(values).all(1)
610+
611+
df[row_mask]
588612
589613
The :meth:`~pandas.DataFrame.where` Method and Masking
590614
------------------------------------------------------

doc/source/v0.13.0.txt

+16
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,22 @@ Experimental
516516
For more details see the :ref:`indexing documentation on query
517517
<indexing.query>`.
518518

519+
- DataFrame now has an ``isin`` method that can be used to easily check whether the DataFrame's values are contained in an iterable. Use a dictionary if you'd like to check specific iterables for specific columns or rows.
520+
521+
.. ipython:: python
522+
523+
df = pd.DataFrame({'A': [1, 2, 3], 'B': ['d', 'e', 'f']})
524+
df.isin({'A': [1, 2], 'B': ['e', 'f']})
525+
526+
The ``isin`` method plays nicely with boolean indexing. To get the rows where each condition is met:
527+
528+
.. ipython:: python
529+
530+
mask = df.isin({'A': [1, 2], 'B': ['e', 'f']})
531+
df[mask.all(1)]
532+
533+
See the :ref:`documentation<indexing.basics.indexing_isin>` for more.
534+
519535
.. _whatsnew_0130.refactoring:
520536

521537
Internal Refactoring

0 commit comments

Comments
 (0)