Pandas or pyspark span column creation

  Kiến thức lập trình

I’ve sample data in the form :

week    year    flag_1  flag_2
26      2022    0        0
27      2022    1        0
28      2022    0        0
2       2023    0        1
3       2023    1        0
4       2023    0        0
5       2023    1        1
6       2023    0        1
7       2023    0        0
8       2023    0        0
9       2023    0        0
10      2023    0        1
11      2023    0        1

I want to create two new columns span_flag_1, span_flag_2.

span_flag_1 will contain 1 if flag_1 == 1, and for all the other rows, span_flag_1 will be
max(num weeks since last flag_1 = 1, num weeks till next flag_1 = 1)

How can i do the same?

LEAVE A COMMENT