polars.DataFrame.sample

DataFrame.sample(n: Optional[int] = None, frac: Optional[float] = None, with_replacement: bool = False, shuffle: bool = False, seed: Optional[int] = None) polars.internals.frame.DF

Sample from this DataFrame by setting either n or frac.

Parameters
n

Number of samples < self.len() .

frac

Fraction between 0.0 and 1.0 .

with_replacement

Sample with replacement.

shuffle

Shuffle the order of sampled data points.

seed

Initialization seed. If None is given a random seed is used.

Examples

>>> df = pl.DataFrame(
...     {
...         "foo": [1, 2, 3],
...         "bar": [6, 7, 8],
...         "ham": ["a", "b", "c"],
...     }
... )
>>> df.sample(n=2, seed=0)  
shape: (2, 3)
┌─────┬─────┬─────┐
│ foo ┆ bar ┆ ham │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ str │
╞═════╪═════╪═════╡
│ 3   ┆ 8   ┆ c   │
├╌╌╌╌╌┼╌╌╌╌╌┼╌╌╌╌╌┤
│ 2   ┆ 7   ┆ b   │
└─────┴─────┴─────┘