- Я хочу знать, превышает ли количество уникальных значений в группе 1, и
- Я хочу знать, содержит ли группа определенное значение.
Предположим, у меня есть этот фрейм данных:
Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌──────┬──────────┬─────────┐
│ item ┆ location ┆ store │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════════╪═════════╡
│ 0 ┆ new york ┆ store 1 │
│ 0 ┆ boston ┆ store 1 │
│ 1 ┆ boston ┆ store 1 │
│ 1 ┆ boston ┆ store 2 │
│ 0 ┆ ohio ┆ store 1 │
│ 0 ┆ ohio ┆ store 3 │
└──────┴──────────┴─────────┘
""")
Код: Выделить всё
df.filter(
(pl.col("store").n_unique() > 1).over("item", "location")
)
Код: Выделить всё
┌──────┬──────────┬─────────┐
│ item ┆ location ┆ store │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════════╪═════════╡
│ 1 ┆ boston ┆ store 1 │
│ 1 ┆ boston ┆ store 2 │
│ 0 ┆ ohio ┆ store 1 │
│ 0 ┆ ohio ┆ store 3 │
└──────┴──────────┴─────────┘
Я попробовал это (и его варианты):
Код: Выделить всё
df.filter(
(pl.col("store").n_unique() > 1).over("item", "location")
& (pl.col("store").is_in(["store 3"]))
)
Код: Выделить всё
┌──────┬──────────┬─────────┐
│ item ┆ location ┆ store │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════════╪═════════╡
│ 0 ┆ ohio ┆ store 3 │
└──────┴──────────┴─────────┘
Код: Выделить всё
┌──────┬──────────┬─────────┐
│ item ┆ location ┆ store │
│ --- ┆ --- ┆ --- │
│ i64 ┆ str ┆ str │
╞══════╪══════════╪═════════╡
│ 0 ┆ ohio ┆ store 1 │
│ 0 ┆ ohio ┆ store 3 │
└──────┴──────────┴─────────┘
Подробнее здесь: https://stackoverflow.com/questions/770 ... -in-polars
Мобильная версия