Skip to content
Tutorials Center
My learning

Topics

Lesson 11 of 37 ยท Expressions

Expression Basics

Learn Polars expressions from the ground up: pl.col, pl.lit, arithmetic, aliasing, aggregations, chaining, expression expansion, and why expressions are fast.

Expressions are the central idea of Polars. An expression is a description of a computation on columns, a recipe that is not run until a context such as select, with_columns, filter or agg evaluates it. Because Polars sees the whole recipe before it runs, it can optimize, share work between expressions, and run them in parallel.

Building blocks

The two most basic expressions are pl.col(name) (refer to a column) and pl.lit(value) (a constant). Everything else is built by calling methods and operators on them.

Python Columns, literals and arithmetic

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Printing expr shows the recipe ([(col("price")) * (col("qty"))]). It is only inside select that data comes out. Python operators + - * / // % ** all work, and Python numbers are automatically wrapped as literals.

Naming results with alias

A result is named after the first column involved unless you say otherwise. Use alias for clear names, or name.prefix, name.suffix and name.map to rename many at once.

Python Naming

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keyword arguments are the shortest way to name outputs: select(total=...) and with_columns(total=...).

Methods chain left to right

Each method returns a new expression, so you read a chain like a sentence.

Python Chaining methods

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Read that as: take x, fill missing with 0, clip negatives to 0, square root, round to one decimal.

Aggregation expressions

Some expressions reduce a column to one value: sum, mean, median, min, max, std, var, count, n_unique, first, last, quantile. In select they yield a one-row result; in group_by().agg they yield one value per group. In with_columns a single value is broadcast to every row.

Python Aggregations and broadcasting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Centering a column by subtracting its own mean is a perfect expression use case: the mean is a single value, the subtraction is per row, and no loops are involved.

Expression expansion: one recipe, many columns

An expression may stand for several columns at once. pl.col("a", "b"), pl.all(), pl.col(pl.Float64) and regexes expand into one output per matching column, and the recipe applies to each.

Python Expansion

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Expansion is what makes Polars code short: instead of writing the same line for twenty columns, you write the recipe once. Column Selectors shows a more readable way to pick the columns.

Comparison and Boolean expressions

Comparisons produce Boolean expressions, which you can use in filter, in with_columns as flags, or in when/then (When, Then, Otherwise).

Python Boolean expressions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Summing a Boolean column counts the Trues, and its mean is the share of Trues.

Expressions are lazy, even in eager mode

An expression never touches data until it is evaluated. This is why the same expression works in a DataFrame context and in a LazyFrame one. It also means you can store recipes in variables, reuse them, and pass them to functions:

Python
is_adult = pl.col("age") >= 18

def total_with_tax(rate: float) -> pl.Expr:
    return (pl.col("price") * pl.col("qty") * (1 + rate)).alias("total")

Functions that return expressions are the Polars way to reuse logic. They stay inside the fast engine, unlike a Python function applied row by row.

Avoid map_elements (applying a Python function to each value) unless nothing else works. It runs Python code per row, ignores parallelism, and can be hundreds of times slower than the built-in expression.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is pl.col('price') * pl.col('qty') by itself?

    Expressions describe computations. Data appears only when a context such as select or with_columns evaluates them.
  2. 2. In df.with_columns(pl.col('score') - pl.col('score').mean()), what does the mean do?

    An aggregation returns one value, which Polars broadcasts when combined with a full-length column.
  3. 3. What does pl.col('a', 'b') * 10 do?

    Expression expansion applies the recipe to each named column and gives one output per column.
  4. 4. Which is the preferred way to name a computed column compactly?

    Keyword arguments in select and with_columns name the output. alias() does the same inside an expression.

Summary

  • An expression is a recipe built from pl.col, pl.lit, operators and methods; contexts evaluate it.
  • Name outputs with alias, name.* methods, or keyword arguments.
  • Aggregation expressions return one value that is broadcast against full columns.
  • One expression can expand to many columns; write functions that return expressions to reuse logic.

Next: String Expressions