Код: Выделить всё
data = {0: [0, 1, 0, 1, 1, 0, 1, 0, 1, 1],
1: [0, 0, 0, 0, 0, 0, 1, 0, 1, 1],
2: [0, 0, 1, 0, 1, 0, 1, 0, 0, 1],
3: [0, 1, 0, 0, 1, 0, 0, 0, 1, 0],
4: [0, 1, 1, 0, 1, 0, 0, 0, 1, 1]}
Код: Выделить всё
0 1 2 3 4
0 0 0 0 0 0
1 1 0 0 1 1
2 0 0 1 0 1
3 1 0 0 0 0
4 1 0 1 1 1
5 0 0 0 0 0
6 1 1 1 0 0
7 0 0 0 0 0
8 1 1 0 1 1
9 1 1 1 0 1
Код: Выделить всё
m = df.eq(1)
m.cumsum(axis=1).sub(m.cumsum(axis=1).mask(m).ffill(axis=1).fillna(0)).max(axis=1)
Код: Выделить всё
to get the following result
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