Код: Выделить всё
import polars as pl
df = pl.LazyFrame(
{
'str': ['A', 'B', 'C', 'B', 'A'],
'group': [1,1,2,1,2]
}
)
df_groups = df.group_by('group').agg(pl.col('str').alias('str_list'))
print(df_groups.collect())
Код: Выделить всё
shape: (2, 2)
┌───────┬─────────────────┐
│ group ┆ str_list │
│ --- ┆ --- │
│ i64 ┆ list[str] │
╞═══════╪═════════════════╡
│ 1 ┆ ["A", "B", "B"] │
│ 2 ┆ ["C", "A"] │
└───────┴─────────────────┘
Код: Выделить всё
pre = 'A'
succ = 'B'
df_groups_filtered = df_groups.filter(
pl.col('str_list').map_elements(
lambda str_list:
pre in str_list and succ in str_list and
str_list.to_list().index(pre) < str_list.to_list().index(succ)
)
)
df_groups_filtered.collect()
Код: Выделить всё
shape: (1, 2)
┌───────┬─────────────────┐
│ group ┆ str_list │
│ --- ┆ --- │
│ i64 ┆ list[str] │
╞═══════╪═════════════════╡
│ 1 ┆ ["A", "B", "B"] │
└───────┴─────────────────┘
Код: Выделить всё
df_groups_filtered = df_groups.filter(
pl.col('str_list').list.contains(pre) & col('str_list').list.contains(succ)
)
Есть ли способы добиться этого с помощью поляров?>
Подробнее здесь: https://stackoverflow.com/questions/726 ... -in-polars