

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: What Is Pandas?
incomplete
2: Series
incomplete
3: DataFrames
incomplete
4: Derived Columns
incomplete
5: Series vs. DataFrame
incomplete
6: Filtering Data
incomplete
7: The Index in Pandas
incomplete
8: Custom Indexes
incomplete
9: Loading Data
incomplete
10: Inspect Head
incomplete
11: Info & Describe
incomplete
12: Inspecting Workflow
incomplete
13: Data Properties
incomplete
14: Inspecting Columns
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
The standard way of filtering out specific rows in Pandas is boolean indexing. You put a boolean expression in the brackets, and Pandas keeps the rows where that expression evaluates to True.
"Boolean expression" means logic that can be applied to each row to yield True or False.
Say you have a DataFrame of movie data, and you want a subset with only the films rated above a certain score:
top_films = df[df["imdb_rating"] > 8.5]
print(top_films)
# title genre imdb_rating status
# 0 The Godfather crime 9.2 streaming
# 2 Pulp Fiction crime 8.9 streaming
# 4 Interstellar sci-fi 8.7 archived
top_films is a new DataFrame with all the same columns, but we filtered it down to only the rows that meet our condition.
Pandas column comparisons like this are vectorized – they operate on entire columns at once, which is much faster than looping through rows individually!
The condition itself is just a Series of True/False values, one per row. You can store it in a variable and reuse it:
# this is the same thing as above
is_top_film = df["imdb_rating"] > 8.5
top_films = df[is_top_film]
print(top_films)
Because True counts as 1 and False as 0, calling .sum() on that boolean Series tells you how many rows match:
top_count = int(is_top_film.sum())
print(top_count)
# 3
SnackStack's operations team needs to monitor device health. Smart fridges running too hot are a safety concern – nobody wants a spontaneous breakfast.
Complete the count_overheating_devices function. It accepts a DataFrame of device readings and a temperature threshold. The operations dashboard wants the headline number and the rows behind it, so it returns two values: the count of overheating devices and a DataFrame with just those rows.