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 With String Methods

Predicate .str methods like these return boolean Series, which means you can use them to filter a DataFrame just like a numeric comparison. If the source can contain missing values, pass na=False or handle them first.

# Flights whose number contains "UA"
united = df[df["flight_number"].str.contains("UA")]

# Flights whose number starts with "DL"
delta = df[df["flight_number"].str.startswith("DL")]

# Flights whose route ends with "-JFK"
to_jfk = df[df["route"].str.endswith("-JFK")]

Case Sensitivity

.str.contains() is case-sensitive by default, so "DELAYED" won't match "delayed". Pass case=False to match regardless of case:

# Matches "delayed", "Delayed", "DELAYED", etc.
df[df["status"].str.contains("delayed", case=False)]

Combining String Filters

Just like numeric conditions, you can combine string conditions with & and |:

# Delta flights that are currently delayed
df[
    df["flight_number"].str.startswith("DL")
    & df["status"].str.contains("delayed", case=False)
]

Assignment

The SnackStack support team needs to find every legacy device still running pre-release firmware so they can prioritize replacements.

Complete the find_legacy_devices function. It accepts a DataFrame, a prefix, and a keyword, and returns only the matching devices.