pythonbeginner

Polars String Operations

Use the Polars .str namespace for fast, vectorised string cleaning and extraction.

python
import polars as pl

df = pl.DataFrame({'email':['Alice@Example.COM','  bob@domain.org ','CHARLIE@TEST.IO'],'code':['US-001','GB-002','DE-003']})

result = df.with_columns([
    pl.col('email').str.strip_chars().str.to_lowercase().alias('email_clean'),
    pl.col('code').str.split('-').list.first().alias('country'),
    pl.col('code').str.extract(r'-(\d+)$', 1).cast(pl.Int32).alias('num'),
])
print(result)

Use Cases

  • data cleaning
  • feature extraction
  • ETL text ops

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.