We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Filtering Data

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

Assignment

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.