Feature engineering
Create derived columns by combining existing columns with arithmetic, comparisons, or conditional methods.
Polars · Class
polars.Expr is Polars' composable expression type for describing column computations. Expressions support operators and methods and can be evaluated in DataFrame, LazyFrame, grouping, and window contexts.
An Expr is Polars' composable description of a column computation. Create expressions with helpers such as pl.col, combine them with operators or methods, and pass them to DataFrame or LazyFrame contexts for evaluation.
class polars.ExprAn expression object used to describe computations; individual Expr methods return Expr unless their documentation specifies another return type.
import polars as pl
df = pl.DataFrame({"value": [1, 2, 3], "group": ["a", "a", "b"]})
result = df.with_columns(
(pl.col("value") * 2).alias("doubled"),
pl.col("value").sum().over("group").alias("group_total"),
)
print(result)Build column expressions, combine them with arithmetic, rename them, and evaluate them in a DataFrame transformation.
Create derived columns by combining existing columns with arithmetic, comparisons, or conditional methods.
Apply aggregations or transformations within groups while preserving expression-based query composition.
Convert expression results to a required Polars data type before downstream operations.
Expr represents a computation that can be used in different Polars contexts. Expressions are composed first and evaluated when supplied to a DataFrame or LazyFrame operation.
Follow-up: How would you create a column expression? Use pl.col("name"), then compose it with methods or operators before evaluating it in a transformation.
Polars overloads common Python operators for Expr, including arithmetic, comparisons, logical operations, and bitwise operations. These operators also have corresponding Expr methods, such as add, eq, and and_.
Follow-up: What is the distinction between logical and integer use of and_? It combines boolean expressions logically, while operating bitwise when the operands are integers.
Expressions can be evaluated in contexts such as DataFrame transformations and grouped or windowed calculations. For example, an aggregation can be applied over a grouping key with sum().over("group").
Follow-up: What is a window expression? It computes an expression over groups while retaining row-level output alignment, unlike a plain aggregation that reduces rows.
polars.colpolars.Expr.addpolars.Expr.aliaspolars.Expr.and_polars.Expr.castpolars.Expr.getpolars.Expr.sumpolars.Expr.over