Derived columns
Create calculated fields from one or more existing columns.
Polars · Module
Adds expression-derived or literal columns to a DataFrame while retaining existing columns. Existing names are replaced, and keyword expressions use their keywords as output names.
Call with_columns with expressions as positional arguments or with keyword expressions. Polars evaluates those expressions to produce a new DataFrame containing the original columns plus the requested results; a result with an existing name replaces that column, while keyword names determine output names.
DataFrame.with_columns(*exprs: IntoExpr | Iterable[IntoExpr], **named_exprs: IntoExpr) → DataFrame| Name | Type | Description |
|---|---|---|
*exprs | IntoExpr | Iterable[IntoExpr] | Column(s) to add, specified as positional arguments. Accepts expression input. Strings are parsed as column names, other non-expression inputs are parsed as literals. |
**named_exprs | IntoExpr | Additional columns to add, specified as keyword arguments. The columns will be renamed to the keyword used. |
DataFrame — a new DataFrame with the columns added.
import polars as pl
df = pl.DataFrame({"a": [1, 2], "b": [10, 20]})
result = df.with_columns(
(pl.col("a") * 2).alias("a_doubled"),
total=pl.col("a") + pl.col("b"),
)
print(result)Adds two derived columns while retaining the original columns.
Create calculated fields from one or more existing columns.
Convert a column's data type while preserving the rest of the frame.
Add several named results compactly with keyword expressions.
with_columns return?with_columns returns a new DataFrame containing the added columns. Existing columns remain available, and an added column with an existing name replaces that column.
Follow-up: How can output names be assigned without calling alias? Use keyword expressions, because the keyword becomes the column name.
Positional arguments accept expression inputs, and multiple expressions can be supplied either individually or through an iterable. Keyword expressions provide an additional way to add and name columns.
Follow-up: What happens to a non-expression positional value? It is parsed as a literal, whereas a string is interpreted as a column name.
with_columns copy existing data?The method creates a new DataFrame without copying the existing data. This preserves the original frame's data storage while presenting the requested columns in the result.
Follow-up: Which related method changes expression execution order? with_columns_seq runs expressions sequentially instead of in parallel.
DataFrame.with_columns_seqDataFrame.selectpolars.col