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

Polars · Module

df.sample

Samples rows from a Polars DataFrame using either a requested count or fraction. Options control duplicate selection, output ordering, and reproducibility.

How it works

Call sample to return randomly selected rows, choosing either an exact count with n or a proportion with fraction. Optional replacement, ordering, and random-seed controls determine duplicates, row order, and reproducibility.

Syntax

DataFrame.sample(n: int | Series | None = None, *, fraction: float | Series | None = None, with_replacement: bool = False, shuffle: bool | None = None, seed: int | None = None) → DataFrame

Parameters

NameTypeDescription
nint | Series | NoneNumber of items to return; cannot be used with fraction. Defaults to 1 if fraction is None.
fractionfloat | Series | NoneFraction of items to return; cannot be used with n.
with_replacementboolAllow values to be sampled more than once.
shufflebool | NoneDetermines the order of the sampled rows.
seedint | NoneSeed for the random number generator.

Returns

DataFrame

Key points

  • Use n to request a specific number of sampled rows.
  • Use fraction when the sample size should be expressed as a proportion.
  • Enable with_replacement to permit duplicate row selections.
  • Set shuffle=True when the sampled output must be explicitly shuffled.
  • Set shuffle=False to retain the sampled rows' original relative ordering.
  • The default shuffle=None prioritizes performance and makes no row-order guarantee.

Common mistakes

  • Do not provide n and fraction together; they are mutually exclusive.
  • Leaving seed unset makes repeated calls non-reproducible because each call receives a new random seed.

Example: Sample a fixed number of rows

import polars as pl

df = pl.DataFrame({"id": [1, 2, 3, 4], "value": [10, 20, 30, 40]})
sample = df.sample(n=2, seed=7)
print(sample)

Creates a two-row reproducible sample from a DataFrame.

When to use it

Random row subset

Create a fixed-size subset for exploratory analysis or a lightweight preview.

Fractional sampling

Generate a proportionally sized subset from datasets of varying lengths.

Reproducible sampling

Produce repeatable examples or tests by supplying a fixed seed.

Sampling with replacement

Construct resampled datasets where observations may appear multiple times.

Interview questions

QHow do n and fraction differ?

n requests an exact number of rows, while fraction requests a proportion of the DataFrame. They cannot be supplied together; if fraction is omitted, n defaults to 1.

Follow-up: How would you make a sampling test deterministic? Pass a fixed integer to seed, then reuse the same sampling arguments.

QWhat does with_replacement control?

with_replacement=True allows a row to be selected more than once. With the default False, sampling does not permit repeated selections.

Follow-up: When is replacement useful? It supports bootstrap-style resampling, where duplicate observations are expected.

QWhat does shuffle mean for the returned row order?

shuffle=True explicitly shuffles sampled rows. shuffle=False preserves their relative order from the source DataFrame, while None provides no ordering guarantee and uses the most performant algorithm.

Follow-up: Which setting should you choose when row order matters? Use True for explicit shuffled order or False to retain source-relative order.

  • polars.DataFrame.filter
  • polars.DataFrame.head
  • polars.DataFrame.limit
  • polars.DataFrame.sample

Source: polars.DataFrame.sample. Checked 2026-10-15.