Random row subset
Create a fixed-size subset for exploratory analysis or a lightweight preview.
Polars · Module
Samples rows from a Polars DataFrame using either a requested count or fraction. Options control duplicate selection, output ordering, and reproducibility.
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.
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| Name | Type | Description |
|---|---|---|
n | int | Series | None | Number of items to return; cannot be used with fraction. Defaults to 1 if fraction is None. |
fraction | float | Series | None | Fraction of items to return; cannot be used with n. |
with_replacement | bool | Allow values to be sampled more than once. |
shuffle | bool | None | Determines the order of the sampled rows. |
seed | int | None | Seed for the random number generator. |
DataFrame
n to request a specific number of sampled rows.fraction when the sample size should be expressed as a proportion.with_replacement to permit duplicate row selections.shuffle=True when the sampled output must be explicitly shuffled.shuffle=False to retain the sampled rows' original relative ordering.shuffle=None prioritizes performance and makes no row-order guarantee.n and fraction together; they are mutually exclusive.seed unset makes repeated calls non-reproducible because each call receives a new random seed.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.
Create a fixed-size subset for exploratory analysis or a lightweight preview.
Generate a proportionally sized subset from datasets of varying lengths.
Produce repeatable examples or tests by supplying a fixed seed.
Construct resampled datasets where observations may appear multiple times.
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.
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.
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.filterpolars.DataFrame.headpolars.DataFrame.limitpolars.DataFrame.sample