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

Polars · Module

df.head

Returns the first n rows of a Polars DataFrame. By default, it returns five rows; a negative n omits that many rows from the end.

How it works

Call head on a DataFrame to obtain a new DataFrame containing the first requested rows. The default is five rows; passing a negative number instead excludes that many rows from the end, which is useful when inspecting nearly all data while omitting a tail.

Syntax

DataFrame.head(*n: int = 5*) → DataFrame

Parameters

NameTypeDescription
nint = 5Number of rows to return. A negative value returns all rows except the last abs(n).

Returns

A DataFrame containing the selected leading rows.

Key points

  • Returns the leading n rows of the DataFrame.
  • The signature accepts an integer n and defaults it to 5.
  • The n argument controls how many rows the result should contain.
  • Negative values exclude the corresponding number of rows from the DataFrame's end.

Common mistakes

  • A negative n does not mean “take rows from the end”; it returns every row except the last abs(n).

Example: Inspect the first rows

import polars as pl

df = pl.DataFrame({"id": [1, 2, 3], "name": ["Ada", "Linus", "Grace"]})
print(df.head(2))

Creates a DataFrame and returns its first two rows.

When to use it

Preview data

Quickly inspect the beginning of a newly loaded or transformed table.

Validate transformations

Check whether an operation produced the expected leading records.

Exclude trailing rows

Display nearly all rows while excluding a known trailing portion.

Interview questions

QWhat does DataFrame.head return, and what is its default?

DataFrame.head returns the first n rows as a DataFrame, with n defaulting to 5. It is useful for quickly inspecting the beginning of a table.

Follow-up: What does head(-3) do? It returns all rows except the final three.

QWhat is the purpose of the n parameter?

The method accepts one parameter, n, which specifies the number of rows to return. The result preserves the DataFrame form rather than returning a Series or scalar.

Follow-up: How would you inspect only one column? Select that column first, then call head on the resulting DataFrame.

QHow does negative n change the behavior?

A negative n is supported and changes the operation to exclude rows from the end. For example, head(-3) returns all rows other than the last three.

Follow-up: What happens if the exclusion count is larger than the DataFrame? Verify the behavior with a small test DataFrame rather than assuming a fixed output shape.

  • polars.DataFrame.limit
  • polars.head
  • polars.Expr.head
  • polars.dataframe.group_by.GroupBy.head

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