Column projection
Selecting a subset of fields from a DataFrame.
Polars · Function
Creates a Polars expression representing one or more DataFrame columns. Use it inside contexts such as select and with_columns to reference, transform, or expand columns.
pl.col creates a Polars expression that refers to one or more DataFrame columns. The expression is evaluated inside a context such as select or with_columns, where it can be combined with transformations, arithmetic, and other expressions.
polars.col(name: str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType], more_names: str | PolarsDataType) → Expr| Name | Type | Description |
|---|---|---|
name | str | PolarsDataType | Iterable[str] | Iterable[PolarsDataType] | Column name, datatype, iterable of names or datatypes, or a regular expression identifying the column(s). |
more_names | str | PolarsDataType | Additional column names or datatypes supplied positionally. |
Expr
pl.col to construct a column expression rather than extracting a Series immediately.col as a column-expression factory rather than technically a function.col.foo.^ and $.import polars as pl
df = pl.DataFrame({"name": ["Ada", "Linus"], "age": [36, 55]})
result = df.select(
pl.col("name"),
pl.col("age") + 1,
)
print(result)Selects the name column and builds a second expression that adds one to every age.
Selecting a subset of fields from a DataFrame.
Building arithmetic, comparison, or string transformations.
Applying one operation to a group of selected columns.
pl.col return, and when is it evaluated?pl.col returns an Expr representing one or more DataFrame columns. It is not evaluated immediately; a DataFrame context such as select executes the expression.
Follow-up: How can one expression target several columns? Pass multiple names, use a regular expression, a datatype, or the wildcard selector.
pl.col?Pass multiple column names as positional arguments, for example pl.col("x", "y"). Polars expands the expression across those selected columns in the surrounding context.
Follow-up: What alternative syntax is documented for a single column? Attribute lookup such as pl.col.foo creates the same expression as pl.col("foo").
pl.col accept?The first argument can be a column name, datatype, iterable of names or datatypes, or a regular expression. A regular-expression selector must be anchored with ^ and $.
Follow-up: What concise selector represents every column? Use the wildcard *.
polars.Exprpolars.DataFrame.selectpolars.DataFrame.with_columnspolars.col.ColumnFactory