Pandas Lesson 4 Search and Filter a Series

  1. Create a Sample Series for this section
  2. Search/Filter a Series by index with iloc
  3. Search/Filter a Series by label with loc
  4. Search/Filter a Series by value
  5. Search/Filter a Series by value using the where() method
  6. Search/Filter a Series by value using the isin() method
  7. Search/Filter a Series by value using loc() method. (return bool results)
  8. Search/Filter a Series by value using loc() method. (return row values)

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
grades = pd.Series([97,93,100,86,100,86,93,84,99],name = "history_grades", 
                   index = ["Henry","John","Jane","Henry","John","Jane","Henry","John","Jane"])
#display grades series
grades
Notebook Output

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.
s = grades.iloc[0]

# 2) Using iloc for multiple indexes: This example returns the first value and the fourth value of the Series.
s = grades.iloc[[0,5]]
print(s)

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

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
s = grades.loc["Henry"]
print(s)
# 2) Using loc to search for multiple index labels
s = grades.loc[["Henry","Jane"]]
print(s)
Notebook Output

4) Search/Filter a Series by value

Series can be searched by value.

# 1) Filter all grades where value equals 100
s = grades[grades == 100]
print(s)
Notebook Output

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
s = grades.where(grades < 90).dropna()
print(s)
Notebook Output

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
s = grades[grades.isin([100,99])]
print(s)
Notebook Output

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)
Notebook Output

8) Search/Filter a Series by value using loc() method. (return row values)

#Filter values using loc to get row values. drop n/a rows.
s = grades.loc["Henry"]
t = s.where(s > 90).dropna()
t
Notebook Output