I'm trying to make a program that finds consecutive rows that meet some conditions. For example, if there's a dataframe that looks like this:
df = pd.DataFrame([1,1,2,-13,-4,-5,6,17,8,9,-10,-11,-12,-13,14,15],
index=[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15],
columns=['value'])
>>> df
value
0 1
1 1
2 2
3 -13
4 -4
5 -5
6 6
7 17
8 8
9 9
10 -10
11 -11
12 -12
13 -13
14 -14
15 15
I want it to return a dataframe that shows rows that meet the conditions below:
1) the order has to be (positive rows) and (negative rows), not the other way around.
2) each positive or negative group of rows has to have at least 3 rows
3) positive and negatives groups have to be adjacent to each other
posIdx, negIdx, posLength, negLength
0 2 3 3 3 # (1,1,2) (-13,-4,-5)
1 9 10 4 5 # (6,17,8,9) (-10,-11,-12,-13,-14)
Are there any simple ways to do this using python or pandas commands?