Filtering Out Values from a Series

To begin discussing the methods of filtering series, it’s important to ensure that you have a clear understanding of what Series are. If you’re not familiar with this data structure, you can check out the Building Pandas series with several datatypes thread to gain a comprehensive understanding of what Series are and how they can be created. Let us now delve into each filtering method and provide examples to illustrate their use.

1. Using a "for" loop:

This method however is slower than the others which are listed below as in this method, each element is individually iterated and then compared with the other series series2 to check if it is present in this series or not.

2. Using the "isin()" method:

  • The isin() method is used to check whether each element in a Pandas DataFrame or Series is contained in a sequence of values which in our case is another series.
  • It returns a boolean mask indicating which values are in the sequence of values passed to the method.

The use of ~ negates the results obtained using isin(), the values which were present in series2 now have a False and the values not in series2 have a True and we use this boolean mask to filter series1 getting those values which were only in series1.

3. Using set "subtraction" operator:

4. Using set "difference()" method:

  • The difference() method is a method in Python that is used to get the set difference of two sets.
  • It returns a set containing elements present in the first set but not in the second set.
1 Like