I have df:
JavaScript
x
7
1
id color val
2
1 y 3
3
1 y 3
4
1 y 3
5
2 y 1
6
2 r 2
7
I want to count the condition that the value in val column is <=3 at least 3 times in the same id and color in a row(in sequence), and when the condition is true, to return the id and color. for example I will get here 1,y thanks
Advertisement
Answer
You can use:
JavaScript
1
10
10
1
N = 3 # number consecutive matches
2
s = (df['val'].le(3) # condition: value ≤ 3
3
.groupby([df['id'], df['color']]) # make groups
4
# is there any occurrence where there are N True in a row?
5
.apply(lambda s: s.rolling(N).sum().eq(N).any())
6
)
7
8
# keep only the True and convert to list
9
s[s].index.to_list()
10
output: [(1, 'y')]
intermediate s
:
JavaScript
1
6
1
id color
2
1 y True
3
2 r False
4
y False
5
Name: val, dtype: bool
6