polars.DataFrame.sample#
- DataFrame.sample(
- n: int | Series | None = None,
- *,
- fraction: float | Series | None = None,
- with_replacement: bool = False,
- shuffle: bool = False,
- seed: int | None = None,
从这个DataFrame中取样。
- Parameters:
- n
返回的项目数量。不能与
fraction一起使用。如果fraction为None,则默认为1。- fraction
返回项目的比例。不能与
n一起使用。- with_replacement
允许值被多次采样。
- shuffle
如果设置为True,采样行的顺序将被随机打乱。如果设置为False(默认值),返回行的顺序将既不稳定也不完全随机。
- seed
随机数生成器的种子。如果设置为None(默认值),则为每次采样操作生成一个随机种子。
示例
>>> 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 │ └─────┴─────┴─────┘