

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
Text data is especially messy. Restaurant names don't follow a consistent format, cuisines are typed by hand, menu items drift... it never ends.
Fortunately, Pandas .str methods let you transform text columns efficiently. You'll often need to:
"TACOS", "Tacos", and "tacos" should all match" Los Pollos Hermanos " should be "Los Pollos Hermanos"Leaving text data in a dirty state causes incorrect groupings, failed joins, and misleading reports!
df["restaurant"].str.upper() # ALL CAPS
df["restaurant"].str.lower() # all lowercase
df["city"].str.title() # Title Case
# " Los Pollos Hermanos " becomes "Los Pollos Hermanos"
df["restaurant"].str.strip()
# Replace spaces with hyphens
df["restaurant"].str.replace(" ", "-", regex=False)
# Returns a boolean Series
df["cuisine"].str.contains("taco")
# Split a reservation email at the @ symbol
df["email"].str.split("@") # Returns a Series of lists
df["email"].str.split("@").str[1] # Returns a Series of just the domain parts
Because each .str method returns a new Series, you can chain them to apply several transformations in a row:
df["restaurant"].str.strip().str.lower().str.replace(" ", "-", regex=False)
Complete the clean_device_registry function. It accepts a DataFrame and returns a copy with two cleaned-up columns.