

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 4
click for more info
Not enough gems
Cost: 6 gems
1: Multiple Conditions
incomplete
2: The Not Operator
incomplete
3: Filter Methods
incomplete
4: Binning
incomplete
5: String Operations
incomplete
6: Filtering With String Methods
incomplete
7: Sorting Data
incomplete
8: More Sorting
incomplete
9: Conditional Updates
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
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")]
.str.contains(sub) – True if the substring appears anywhere.str.startswith(prefix) – True if the string starts with prefix.str.endswith(suffix) – True if the string ends with suffix.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)]
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)
]
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.