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

Polars · Module

df.limit

DataFrame.limit(n) returns the first n rows as a DataFrame, defaulting to five rows. A negative n returns all rows except the last abs(n) rows.

How it works

Call limit on a DataFrame to produce a DataFrame containing a bounded prefix of its rows. A positive n keeps the first n rows, while a negative n removes that many rows from the end instead; the method is equivalent to head.

Syntax

DataFrame.limit(n: int = 5) -> DataFrame

Parameters

NameTypeDescription
nint = 5Number of rows to return. Negative values return all rows except the last abs(n) rows.

Returns

DataFrame

Key points

  • Use limit(n) to keep a leading slice of the DataFrame.
  • limit and head are equivalent DataFrame operations.
  • The n argument controls the requested row count.
  • A negative n excludes abs(n) rows from the end.
  • The default call requests five rows and returns another DataFrame.

Common mistakes

  • Negative values do not select rows from the end; they exclude that many rows from the DataFrame's tail.

Example: Limit rows

import polars as pl

df = pl.DataFrame({"value": [10, 20, 30, 40, 50]})
print(df.limit(3))
print(df.limit(-2))

Returns a new DataFrame containing the first three rows, then all rows except the final two.

When to use it

Show a small leading sample

Previewing a dataset

Keep only an initial row window

Bounding intermediate results

Exclude the final rows with a negative limit

Dropping a known tail

Interview questions

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

DataFrame.limit(n) returns the first n rows as a DataFrame, with n defaulting to 5. It is an alias for DataFrame.head().

Follow-up: What does limit(-n) do? It returns every row except the last abs(n) rows.

QWhat is the parameter and return type of limit?

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

Follow-up: How would you inspect the result without changing the original object? Assign or print the returned DataFrame; the documented operation returns a DataFrame.

QHow does negative n differ from positive n?

A positive limit selects a prefix of the DataFrame. A negative limit instead returns all rows other than the specified number at the end.

Follow-up: What related API provides the same behavior? DataFrame.head() is the documented alias.

  • DataFrame.head

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