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

String Operations

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:

  • Standardize case"TACOS", "Tacos", and "tacos" should all match
  • Remove whitespace" Los Pollos Hermanos " should be "Los Pollos Hermanos"
  • Replace text – swap spaces for hyphens in every restaurant name
  • Extract patterns – pull the domain out of a reservation email

Leaving text data in a dirty state causes incorrect groupings, failed joins, and misleading reports!

Common String Methods

  • Applying uppercase, lowercase, or title case:
    df["restaurant"].str.upper()  # ALL CAPS
    df["restaurant"].str.lower()  # all lowercase
    df["city"].str.title()  # Title Case
    
  • Removing leading/trailing whitespace:
    # " Los Pollos Hermanos " becomes "Los Pollos Hermanos"
    df["restaurant"].str.strip()
    
  • Replacing text:
    # Replace spaces with hyphens
    df["restaurant"].str.replace(" ", "-", regex=False)
    
  • Checking if a string contains a given substring:
    # Returns a boolean Series
    df["cuisine"].str.contains("taco")
    
  • Splitting strings into parts:
    # 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
    

Chaining

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)

Assignment

Complete the clean_device_registry function. It accepts a DataFrame and returns a copy with two cleaned-up columns.