polars.first#

polars.first(*columns: str) Expr[source]#

Get the first value.

This function has different behavior depending on the input type:

  • None -> Takes first column of a context (equivalent to cs.first()).

  • str or [str,] -> Syntactic sugar for pl.col(columns).first().

Parameters:
*columns

One or more column names. If not provided (default), returns an expression to take the first column of the context instead.

Examples

>>> df = pl.DataFrame(
...     {
...         "a": [1, 8, 3],
...         "b": [4, 5, 2],
...         "c": ["foo", "bar", "baz"],
...     }
... )
>>> df.select(pl.first())
shape: (3, 1)
┌─────┐
│ a   │
│ --- │
│ i64 │
╞═════╡
│ 1   │
│ 8   │
│ 3   │
└─────┘
>>> df.select(pl.first("b"))
shape: (1, 1)
┌─────┐
│ b   │
│ --- │
│ i64 │
╞═════╡
│ 4   │
└─────┘
>>> df.select(pl.first("a", "c"))
shape: (1, 2)
┌─────┬─────┐
│ a   ┆ c   │
│ --- ┆ --- │
│ i64 ┆ str │
╞═════╪═════╡
│ 1   ┆ foo │
└─────┴─────┘