Kyle
Kyle

Reputation: 476

Find sequential periods of data in Pandas Dataframe with specific value and group them into sub dataframes

df = pd.DataFrame({'time_seconds':[0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0,1.1,1.2], 'v':[12,3,5,0,0,0,23,4,2,0,0,12,0]})

Out[9]: 
    time_seconds   v
0            0.0  12
1            0.1   3
2            0.2   5
3            0.3   0
4            0.4   0
5            0.5   0
6            0.6  23
7            0.7   4
8            0.8   2
9            0.9   0
10           1.0   0
11           1.1  12
12           1.2   0

With a Dataframe as above, I would like to group the dataframe into separate dataframes where the v is 0. So something like this:

Out[9]: 
    time_seconds   v
3            0.3   0
4            0.4   0
5            0.5   0
Out[10]: 
    time_seconds   v
9            0.9   0
10           1.0   0
Out[11]: 
    time_seconds   v
12           1.2   0

I've been searching through various answers and I just can't seem to wrap my head around groupby, cumsum, shift and diff.

I always get strange results and I can't quite understand what I'm doing wrong... for example I've tried:

stops = df[df.v <=0] 
stops.time_seconds.diff() != 0.1

But I just get empty results. I feel like this should be a very simple task, but for some reason I can't get it.

Upvotes: 0

Views: 91

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You can try cumsum to identify the blocks, then groupby:

s = df['v'].eq(0)

for k, v in df[s].groupby((~s).cumsum()):
    print(v)

Output:

   time_seconds  v
3           0.3  0
4           0.4  0
5           0.5  0
    time_seconds  v
9            0.9  0
10           1.0  0
    time_seconds  v
12           1.2  0

Upvotes: 1

Related Questions