Reputation: 107
This question is a follow-up of the follow question: What is the best way to check if the last rows of a pandas dataframe meet a condition?
But I got stuck trying to modify the answers provided to meet my needs.
Criteria 01 = If the last five (5) consecutive rows(including the last) of singal
are 1, it would return 1.
Criteria 02 = If the last three (3) consecutive rows(including the last) of singal
are 0, it would return 0.
Criteria 03 = Before the first meet of CRITERIA 01 or CRITERIA 02, it would return nan
.
Criteria 04 = Everything else would be the last value of check.
like this:
index signal check
0 1 nan
1 1 nan
2 1 nan
3 1 nan
4 1 1
5 1 1
6 0 1
7 0 1
8 0 0
9 0 0
10 0 0
11 1 0
12 0 0
13 1 0
14 0 0
15 1 0
16 1 0
17 1 0
18 1 0
19 1 1
I would appreciate any kind of help!
Thank you!
Upvotes: 0
Views: 71
Reputation: 25239
You need another rolling(3)
as follows
m1 = df.rolling(5).sum().eq(5)
m2 = df.eq(0).rolling(3).sum().eq(3)
df['check'] = df[m1 | m2].ffill()
Out[310]:
signal check
index
0 1 NaN
1 1 NaN
2 1 NaN
3 1 NaN
4 1 1.0
5 1 1.0
6 0 1.0
7 0 1.0
8 0 0.0
9 0 0.0
10 0 0.0
11 0 0.0
12 0 0.0
13 1 0.0
14 0 0.0
15 1 0.0
16 1 0.0
17 1 0.0
18 1 0.0
19 1 1.0
Mask m2
could also be simplified to this
m2 = df.rolling(3).sum().eq(0)
Upvotes: 1
Reputation: 2621
First select the last 5 rows of a dataframe and select the column, then check if all the results are equal to some value (ie: 1 for CRITERIA 1).
import numpy as np
if np.all(df[-5:]['signal'] == 1):
print('CRITERIA 01 is met')
Then you can build the other criterias similarly.
Upvotes: 0