So, there is an initial DF. The first column shows the stage, full cycle 1-5. If suddenly there is not a full cycle in the sample, then all observations of the incomplete cycle are deleted. That is, the input is:

Time X1 X2 1 5 5 2 6 6 3 4 4 4 6 5 5 5 5 1 5 6 2 6 5 3 5 6 1 4 6 2 6 5 3 5 5 4 6 5 5 5 5 

The second cycle is incomplete: 1-3, so all their observations are deleted. And the output is:

 Time X1 X2 1 5 5 2 6 6 3 4 4 4 6 5 5 5 5 1 4 6 2 6 5 3 5 5 4 6 5 5 5 5 
  • Are the values ​​in the “Time” column in one group strictly non-decreasing? - MaxU
  • Yes, strictly non - decreasing - Max52 February

1 answer 1

 In [13]: res = df.groupby(df['Time'].diff().fillna(1).lt(1).cumsum()).filter(lambda x: len(x)>=5) In [14]: res Out[14]: Time X1 X2 0 1 5 5 1 2 6 6 2 3 4 4 3 4 6 5 4 5 5 5 8 1 4 6 9 2 6 5 10 3 5 5 11 4 6 5 12 5 5 5 

Step by Step:

 In [16]: df['Time'].diff().fillna(1) Out[16]: 0 1.0 1 1.0 2 1.0 3 1.0 4 1.0 5 -4.0 6 1.0 7 1.0 8 -2.0 9 1.0 10 1.0 11 1.0 12 1.0 Name: Time, dtype: float64 In [17]: df['Time'].diff().fillna(1).lt(1) Out[17]: 0 False 1 False 2 False 3 False 4 False 5 True 6 False 7 False 8 True 9 False 10 False 11 False 12 False Name: Time, dtype: bool In [18]: df['Time'].diff().fillna(1).lt(1).cumsum() Out[18]: 0 0 1 0 2 0 3 0 4 0 5 1 6 1 7 1 8 2 9 2 10 2 11 2 12 2 Name: Time, dtype: int32 

then we can group DataFrame by the result of previous manipulations ...