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
str.to_lowercasenormalizes case.str.split(...).list.last()pulls the domain.str.containsandstr.len_charstest and measure.
Keywords and builtins used here
as
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.
Step 1 of 3 in Text, lists & structs, step 20 of 32 in Polars.