I have a column A in a dataframe and I want to subset rows whenever they are in a specific range consecutively. For e.g, if the nth row is within (4.8,5.3) range and the n+1th row is within (4.8,5.3) range and the n+2nd row is in (-10.3,-9.7)
Col A ColB
13.8 A
20.2 A
15.3 A
4.9 A
5.2 A
-9.8 A
20.1 A
4.5 A
3.2 A
-9.8 A
5 A
4.8 A
-10 A
12.2 A
For the above input, I would like the following subset of rows in another dataframe (the 3 consecutive rows which have values in the specified range):
ColA ColB
4.9 A
5.2 A
-9.8 A
5 A
4.8 A
-10 A
I'm able to figure this out with a for loop but my dataframe has more than 70000 rows and it is very slow. (I have given only a sample dataframe here). Is there any more pythonic way to do this? Thanks!