Код: Выделить всё
import polars as pl
df = pl.from_repr("""
┌─────┬─────┬─────┬─────┬─────┐
│ 0 ┆ 1 ┆ 2 ┆ 3 ┆ 4 │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ i64 ┆ i64 ┆ i64 ┆ i64 ┆ i64 │
╞═════╪═════╪═════╪═════╪═════╡
│ 0 ┆ 0 ┆ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 0 ┆ 0 ┆ 1 ┆ 1 │
│ 0 ┆ 0 ┆ 1 ┆ 0 ┆ 1 │
│ 1 ┆ 0 ┆ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 0 ┆ 1 ┆ 1 ┆ 1 │
│ 0 ┆ 0 ┆ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 1 ┆ 1 ┆ 0 ┆ 0 │
│ 0 ┆ 0 ┆ 0 ┆ 0 ┆ 0 │
│ 1 ┆ 1 ┆ 0 ┆ 1 ┆ 1 │
│ 1 ┆ 1 ┆ 1 ┆ 0 ┆ 1 │
└─────┴─────┴─────┴─────┴─────┘
""")
Код: Выделить всё
m = df.to_pandas().eq(1)
m.cumsum(axis=1).sub(m.cumsum(axis=1).mask(m).ffill(axis=1).fillna(0)).max(axis=1)
Код: Выделить всё
0 0.0
1 2.0
2 1.0
3 1.0
4 3.0
5 0.0
6 3.0
7 0.0
8 2.0
9 3.0
dtype: float64
Подробнее здесь: https://stackoverflow.com/questions/798 ... -in-polars
Мобильная версия