Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Polars · Function

pl.first

Returns an expression that selects the first column when no names are supplied, or the first value from each named column when names are supplied.

How it works

pl.first returns an expression whose behavior depends on whether column names are supplied. Without names it selects the first column in the current context; with one or more names it selects the first value from each named column.

Syntax

polars.first(*columns: str) → Expr

Parameters

NameTypeDescription
columnsstrOne or more column names.

Returns

Expr

Key points

  • pl.first is an expression-level helper for retrieving either a first column or first values.
  • Omitting columns selects the first column from the current expression context.
  • Providing names extracts the first value from every named column.
  • The columns argument accepts one or more column names.
  • The documented no-argument usage returns a full column rather than reducing the DataFrame to one row.
  • The named usage returns first values, including one value per supplied column.

Common mistakes

  • Calling the function with and without column arguments produces different shapes and meanings: a whole first column versus first values.

Example: Select a first column or value

import polars as pl

df = pl.DataFrame({"a": [1, 8, 3], "b": [4, 5, 2]})

print(df.select(pl.first()))
print(df.select(pl.first("b")))

The first selection returns column a, while the named selection returns the first value from column b.

When to use it

Select the leading column

Use pl.first() in select expressions when the first column of the current context is needed.

Extract initial values

Use pl.first("name", "score") to retrieve the initial row's values from selected columns.

Project multiple first values

Use several column names when building a compact one-row selection from a DataFrame.

Interview questions

QHow does pl.first() behave when called without arguments?

With no arguments, pl.first() returns an expression that takes the first column of the context. For example, selecting it from a DataFrame returns the complete first column.

Follow-up: What happens with multiple names? pl.first("a", "c") returns one first value for each named column, producing a one-row result.

QWhat does supplying a column name change?

Passing column names changes the operation from column selection to value extraction. pl.first("b") selects the first value of column b.

Follow-up: How can the result be verified? Inspect the selected DataFrame: the named form has one row, whereas the no-argument form has the input height.

QWhat does the columns parameter represent?

The function accepts one or more column names and returns an Expr. Multiple names are handled together, so each named column contributes its first value to the result.

Follow-up: What should be checked when using several names? Confirm that every supplied name exists and that the resulting columns correspond to those names.

  • polars.last
  • polars.Expr.first

Source: polars.first — Polars documentation. Checked 2026-10-15.