I am unsure how best to create a row-based Boolean mask from column-based Boolean mask.
I am trying to extract defined length (e.g. 1,2,3 etc.) positive (or negative) run sequences from 'B' for one 'SN' into a new mask.
I have now implemented a simple mask (below) and on top of that a complicated for-loop with several if-statements to do this. Is there some more elegant way to create mask-on-mask in Pandas?
df = pd.DataFrame({
"SN" : ["66", "66", "77", "77", "77", "77", "77"],
"B" : [-1, 1, 2, 3, 1, -1, 1]
})
mask = df['B'] > 0
The output with simple mask is
SN B
0 66 -1
1 66 1
2 77 2
3 77 3
4 77 1
5 77 -1
6 77 1
0 False
1 True
2 True
3 True
4 True
5 False
6 True
The desired output is
defined_min_length = 2
0 False
1 False
2 True
3 True
4 True
5 False
6 False
defined_min_length = 3
0 False
1 False
2 True
3 True
4 True
5 False
6 False
defined_min_length = 4
0 False
1 False
2 False
3 False
4 False
5 False
6 False
Edit: Try to fix question's ambiguity. The key point is the "defined length". E.g. in the example defined lenght = 4 would yield all False as there is no positive run with length 4 in the data frame for any equipment (with same 'SN').
Edit 2: I reformulate the original question. Adding variable defined_min_length to indicate the desired run-length.