2

I have a series that looks like the following:

Time Step
0   0
1   1
2   2
3   2
4   2
5   3
6   0
7   1
8   2
9   2
10  2
11  3

I want to use Pandas to perform a conditional rolling count of each block of time that contains step = 2 and output the count to a new column. I have found answers on how to do conditional rolling counts (Pandas: conditional rolling count), but I cannot figure out how to count the sequential runs of each step as a single block. The output should look like this:

Time Step Run_count
0   0 
1   1
2   2     RUN1
3   2     RUN1
4   2     RUN1
5   3
6   0
7   1
8   2     RUN2
9   2     RUN2
10  2     RUN2
11  3

1 Answers1

0

Let's try:

s = df.Step.where(df.Step.eq(2))
df['Run_count'] = s.dropna().groupby(s.isna().cumsum()).ngroup()+1

Output:

    Time  Step  Run_count
0      0     0        NaN
1      1     1        NaN
2      2     2        1.0
3      3     2        1.0
4      4     2        1.0
5      5     3        NaN
6      6     0        NaN
7      7     1        NaN
8      8     2        2.0
9      9     2        2.0
10    10     2        2.0
11    11     3        NaN
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74