typestar

Polars expressions in Python

The expression API that powers every transform.

import polars as pl

df = pl.DataFrame({
    "name": ["ada", "grace", "katherine"],
    "score": [95, 88, 92],
})

ranked = df.with_columns(
    (pl.col("score") / 100).alias("ratio"),
    pl.col("name").str.to_uppercase().alias("label"),
    (pl.col("score") >= 90).alias("honors"),
)

bumped = df.with_columns(pl.col("score") + 5)

How it works

  1. with_columns adds computed columns.
  2. pl.col('score') refers to a column in an expression.
  3. alias names the result; string and boolean ops chain.

Keywords and builtins used here

The run, in numbers

Lines
14
Characters to type
312
Tokens
130
Three-star pace
105 tpm

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

Type this snippet

Step 1 of 4 in Expressions, step 5 of 32 in Polars.

← Previous Next →