Stack Exchange
How to fill values in a pandas Series in positions between specific "start" and "stop" markers?
I have a DataFrame that is a follows:
df[16820:16830]
       data0  start_stop
16820      1           0
16821      1           1
16822      1           0
16823      1           0
16824      1           0
16825      1          -1
16826      0           0
16827      0           0
16828      1           1
16829      0           0
16830      1          -1

What I need to do is mark values between 1 and -1 in the start_stop columns as valid(1 means 'start', -1 means 'stop') and values between -1 and 1 as invalid (rubbish that I will later discard).
Is there any efficient way to do this instead of iterating with loops over the whole dataframe?
The end result would look like this:
       data0  start_stop  valid
16820      1           0   False
16821      1           1   True
16822      1           0   True
16823      1           0   True
16824      1           0   True
16825      1          -1   False
16826      0           0   False
16827      0           0   False
16828      1           1   True
16829      0           0   True
16830      1          -1   False
...

The relevant loop that would achieve it is, i think this:
df = df.reset_index(drop=True)
value = False
for i in range(0,df.shape[0]):
    if df.loc[i, 'start_stop'] == 1:
        df.loc[i,'valid'] = True
        value = True
    elif df.loc[i, 'start_stop'] == -1:
        df.loc[i, 'valid'] = False
        value = False
    if df.loc[i, 'start_stop'] == 0:
        df.loc[i, 'valid'] = value

Thanks!

why is the last value from start_stop -1 in the output?

@anky_91my mistake, edited.

This should work
df['valid'] = df.start_stop.cumsum()

Then
df['valid'] = df['valid'].apply(lambda x: True if x==1 else False)

df

    start_stop  valid
0            0  False
1            1   True
2            0   True
3            0   True
4            0   True
5           -1  False
6            0  False
7            0  False
8            1   True
9            0   True
10          -1  False


This is how it's done. I was mistakenly trying to find a way through numpy.trapz(). Thanks!

df['valid']=df['start_stop'].cumsum().astype(bool) would be more convenient
