Код: Выделить всё
>>> import polars as pl
>>> df = pl.select(pl.int_range(1,100).sample(5).alias('a'))
>>> df.with_columns(pl.int_ranges(3, 5).list.sample(2))
Код: Выделить всё
>>> df = pl.select(pl.int_range(1,100).sample(5).alias('a'))
>>> df.with_columns(pl.int_ranges(1, 5).list.sample(2))
shape: (5, 2)
┌─────┬───────────┐
│ a ┆ literal │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪═══════════╡
│ 46 ┆ [3, 2] │
│ 5 ┆ [3, 2] │
│ 41 ┆ [3, 2] │
│ 95 ┆ [3, 2] │
│ 84 ┆ [3, 2] │
└─────┴───────────┘
Код: Выделить всё
>>> df.with_columns(pl.int_ranges(1, 5).list.sample(2))
shape: (5, 2)
┌─────┬───────────┐
│ a ┆ literal │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪═══════════╡
│ 46 ┆ [3, 4] │
│ 5 ┆ [2, 1] │
│ 41 ┆ [4, 3] │
│ 95 ┆ [1, 4] │
│ 84 ┆ [1, 3] │
└─────┴───────────┘
Код: Выделить всё
>>> df.with_columns(pl.int_ranges(pl.lit(1), pl.lit(5)).list.sample(2))
shape: (5, 2)
┌─────┬───────────┐
│ a ┆ literal │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪═══════════╡
│ 46 ┆ [1, 4] │
│ 5 ┆ [1, 4] │
│ 41 ┆ [1, 4] │
│ 95 ┆ [1, 4] │
│ 84 ┆ [1, 4] │
└─────┴───────────┘
Код: Выделить всё
>>> df.with_columns(pl.int_ranges(1, 5).alias('range')).with_columns(pl.col('range').list.sample(2))
shape: (5, 2)
┌─────┬───────────┐
│ a ┆ range │
│ --- ┆ --- │
│ i64 ┆ list[i64] │
╞═════╪═══════════╡
│ 46 ┆ [4, 3] │
│ 5 ┆ [2, 1] │
│ 41 ┆ [2, 1] │
│ 95 ┆ [2, 3] │
│ 84 ┆ [2, 3] │
└─────┴───────────┘
Подробнее здесь: https://stackoverflow.com/questions/798 ... st-samplen
Мобильная версия