typestar

Polars string expressions in Python

The str namespace for text columns in Polars.

import polars as pl

df = pl.DataFrame({
    "email": ["Ada@Mail.io", "grace@navy.mil", "kat@code.dev"],
})

parts = df.with_columns(
    pl.col("email").str.to_lowercase().alias("normalized"),
    pl.col("email").str.split("@").list.last().alias("domain"),
    pl.col("email").str.contains("mail").alias("is_mail"),
    pl.col("email").str.len_chars().alias("length"),
)

How it works

  1. str.to_lowercase normalizes case.
  2. str.split(...).list.last() pulls the domain.
  3. str.contains and str.len_chars test and measure.

Keywords and builtins used here

The run, in numbers

Lines
12
Characters to type
351
Tokens
138
Three-star pace
110 tpm

At the three-star pace of 110 tokens a minute, this run takes about 75 seconds.

Type this snippet

Step 1 of 3 in Text, lists & structs, step 20 of 32 in Polars.

← Previous Next →