The isin and any all methods

These methods enable the user to achieve more with Boolean indexing than the standard operators used in the preceding sections. The isin method takes a list of values and returns a Boolean array with True at the positions within the Series or DataFrame that match the values in the list. This enables the user to check for the presence of one or more elements within a Series. Here is an illustration using Series:

    In[317]:stockSeries=pd.Series(['NFLX','AMZN','GOOG','FB','TWTR'])
               stockSeries.isin(['AMZN','FB'])    Out[317]:0    False
            1     True
            2    False
            3     True
            4    False
            dtype: bool
  

Here, we use the Boolean array to select a sub-series containing the values that we're interested in:

 In [318]: stockSeries[stockSeries.isin(['AMZN','FB'])] ...

Get Mastering pandas - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.