Project selected columns
Reduce a wide table to the fields needed by downstream code or export.
Polars · Module
Selects columns and evaluates Polars expressions to produce a new DataFrame. Inputs may be column names, iterables of names or expressions, and keyword expressions with explicit output names.
Call DataFrame.select with positional column names, lists, or expressions to construct a new DataFrame. Keyword expressions add columns whose output names come from the keyword, while strings identify columns and other non-expression values become literals.
DataFrame.select(exprs: IntoExpr | Iterable[IntoExpr]*, **named_exprs: IntoExpr*) → DataFrame| Name | Type | Description |
|---|---|---|
exprs | IntoExpr | Iterable[IntoExpr] | Columns or expressions to select as positional arguments; strings are treated as column names and other non-expression inputs as literals. |
named_exprs | IntoExpr | Additional expressions supplied by keyword; each output column is named after its keyword. |
DataFrame containing the selected columns and evaluated expressions.
pl.lit(...) when you intend a literal value.import polars as pl
df = pl.DataFrame({"name": ["Ada", "Linus"], "age": [36, 55]})
result = df.select(
pl.col("name"),
(pl.col("age") + 1).alias("next_age"),
label=pl.lit("engineer"),
)
print(result)Selects an existing column, computes a renamed expression, and adds a literal column under a keyword name.
Reduce a wide table to the fields needed by downstream code or export.
Create derived fields while selecting, such as adjusted values or labels.
Attach constants or named expression results during projection.
DataFrame.select do?select creates a new DataFrame containing the requested columns or expression results. It accepts positional arguments, iterable inputs such as a list, and keyword expressions.
Follow-up: How would you add a constant column? Use a literal expression, for example df.select(pl.lit(1).alias("constant")).
select accept?Positional strings are interpreted as column names, and expressions can transform or calculate values. Multiple columns can be supplied either as separate positional arguments or as a list.
Follow-up: How can an expression's output name be controlled? Alias it with .alias(...), or provide it as a keyword expression when selecting.
Keyword expressions add selected outputs and use their keyword names as the resulting column names. The method returns a DataFrame.
Follow-up: What is the lazy equivalent when building an optimized query? Start with a LazyFrame and use its select method, then collect the result.
DataFrame.select_seqpolars.selectpolars.colpolars.litpolars.selectors