Lesson 2 ❮ Lesson List ❮ Top Page
❯ 2.1 Filtering with Boolean
⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳ Video 6m 14s
☷ Interactive readings 5m
While we selected data by specifying a label name before, we can also use a condition for filtering. First, we create a variable to hold each set of criteria independently as a boolean Series.
In this example, we make two sets of criteria for the movie dataset. Familiarizing yourself with some boolean notation such as |, ==, &, can be really handy.
We have successfully filtered the data and all the columns of the DataFrame. We can't easily perform a manual check to determine whether the filter worked correctly. Let's filter both rows and columns with the .loc indexer.
Here's the list of Boolean comparison operators.
== Equal to
!= Not equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
There are three logical operators that are used to compare values. They evaluate expressions down to Boolean values, returning either True or False. These operators are and, or, & not, defined as follows:
Operator What it means Example Symbol
and True if both are true x and y x & y
or True if at least one is true x or y x | y
not True only if false not x ~x