Pandas Lesson 4 Search and Filter a Series

In this section we will review searching and filtering a Pandas Series. Let’s begin by creating the Series we will use in the lessons below.

1) Create Sample Series

#import the pandas library
import pandas as pd

#create the Pandas Series include the Data, Name and index
index = ["Henry","John","Jane","Henry","John","Jane","Henry","John","Jane"])

2) Search/Filter a Series by index with iloc

The iloc method handles as input a single index or multiple indexes as well as slice notation.

# 1) Using iloc for a single index: This example returns the first value of the Series.

# 2) Using iloc for multiple indexes: This example returns the first value and the fourth value of the Series.
print(s)

# 3) Using iloc for a range of indexes:
print('--Start from 1st item and end at 3rd item--')
print(a)
print('--Start after 3rd item to the end of Series--')
print(b)
print("--Start after 2nd item to the 4th of the Series  ")

3) Search/Filter a Series by label with loc

The loc() method takes as input index labels. You can provide 1 label or a list of labels.
# 1) Using loc to search for a single index label
print(s)
# 2) Using loc to search for multiple index labels
print(s)

4) Search/Filter a Series by value

Series can be searched by value.

# 1) Filter all grades where value equals 100
print(s)

5) Search/Filter a Series by value using the where() method

The where() method filters a series by values using expressions and operators.

#Filter grades where value is less than 99 and drop N/A values
print(s)

6) Search/Filter a Series by value using the isin() method

The isin() method takes a list of values as input.

#Filter grades where value equals 100 or 99
print(s)

7) Search/Filter a Series by value using loc() method. (return bool results)

#Filter loc method results by value. returns bool from filter
s = grades.loc["Henry"] > 90
print(s)