Код: Выделить всё
import polars as pl
df = pl.DataFrame({'a':[1,1,1,1,2,2,2,2],'b':[1,2,1,2,1,2,1,2],'c':[10,10,12,13,14,15,16,17]})
print(df)
output:
shape: (8, 3)
┌─────┬─────┬─────┐
│ a ┆ b ┆ c │
│ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╡
│ 1 ┆ 1 ┆ 10 │
│ 1 ┆ 2 ┆ 10 │
│ 1 ┆ 1 ┆ 12 │
│ 1 ┆ 2 ┆ 13 │
│ 2 ┆ 1 ┆ 14 │
│ 2 ┆ 2 ┆ 15 │
│ 2 ┆ 1 ┆ 16 │
│ 2 ┆ 2 ┆ 17 │
└─────┴─────┴─────┘
Я добился этого следующим образом:
Код: Выделить всё
df.select(pl.col("c").cut([11,14]).value_counts().sort().struct.field("count").first())
output:
shape: (1, 1)
┌───────┐
│ count │
│ --- │
│ u32 │
╞═══════╡
│ 2 │
└───────┘
Код: Выделить всё
df.group_by("a").agg(pl.col("c").cut([11,14]).value_counts().sort().struct.field("count").first())
Код: Выделить всё
PanicException: called `Option::unwrap()` on a `None` value
Подробнее здесь: https://stackoverflow.com/questions/784 ... r-a-column