Search this site
Embedded Files
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
AIMD GPDS Courses
  • Home
  • Courses
  • Contact
  • More
    • Home
    • Courses
    • Contact

日本語  ❯

Lesson 2    ❮    Lesson List    ❮    Top Page

❯  2.1  Filtering with Boolean

2.2  Grouping Object

2.3  Aggregation

2.4  Concatenate, Join, Merge

⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺⎺
EXPECTED COMPLETION TIME
❲▹❳  Video   6m 14s
☷  Interactive readings   5m

Filtering with Basic Boolean Condition

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. 

> It is possible to use one long boolean expression in place of several other shorter ones. To replicate the final_crit_a variable  with one long line of code, we can do the followingfinal_crit_a2 = (movie.imdb_score > 8) & (movie.content_rating == 'PG-13') & \
((movie.title_year < 2000) | (movie.title_year > 2009))

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.

Understanding Boolean Comparison Operators

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

©2023. All rights reserved.  Samy Baladram,
Graduate Program in Data Science - GSIS - Tohoku University
Google Sites
Report abuse
Page details
Page updated
Google Sites
Report abuse