Мой фрейм данных выглядит примерно так:
Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌─────┬─────┬─────┬─────────┐
│ id1 ┆ id2 ┆ id3 ┆ call_id │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════════╡
│ 1 ┆ 4 ┆ 9 ┆ null │
│ 1 ┆ 5 ┆ 9 ┆ null │
│ 1 ┆ 5 ┆ 9 ┆ null │
│ 2 ┆ 5 ┆ 9 ┆ null │
│ 2 ┆ 6 ┆ 9 ┆ 2 │
│ 2 ┆ 7 ┆ 10 ┆ null │
│ 3 ┆ 7 ┆ 11 ┆ null │
│ 3 ┆ 7 ┆ 12 ┆ null │
│ 3 ┆ 7 ┆ 13 ┆ 7 │
│ 3 ┆ 8 ┆ 13 ┆ null │
└─────┴─────┴─────┴─────────┘
""")
Код: Выделить всё
┌─────┬─────┬─────┬─────────┐
│ id1 ┆ id2 ┆ id3 ┆ call_id │
│ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════════╡
│ 1 ┆ 4 ┆ 9 ┆ null │
│ 1 ┆ 5 ┆ 9 ┆ null │
│ 1 ┆ 5 ┆ 9 ┆ null │
│ 2 ┆ 5 ┆ 9 ┆ 2 │
│ 2 ┆ 6 ┆ 9 ┆ 2 │
│ 2 ┆ 7 ┆ 10 ┆ 7 │
│ 3 ┆ 7 ┆ 11 ┆ 7 │
│ 3 ┆ 7 ┆ 12 ┆ 7 │
│ 3 ┆ 7 ┆ 13 ┆ 7 │
│ 3 ┆ 8 ┆ 13 ┆ null │
└─────┴─────┴─────┴─────────┘
Код: Выделить всё
df.with_columns(pl.col('call_id').backward_fill().over('id1'))
Подробнее здесь: https://stackoverflow.com/questions/794 ... -in-polars
Мобильная версия