I have a data frame as follow
df = pd.DataFrame({'subjects':['A','A','B','C','A','A','B','B','C','C'],
'value':[3,5,5,6,7,2,4,8,9,2]})
subjects value
A 3
A 5
B 5
C 6
A 7
A 2
B 4
B 8
C 9
C 2
Now I want to select those subjects, where at some point value is less than 5 and becomes greater than 5 at some other subsequent point coming after the point where value is less than 5.
In this data frame it will be A and B.
I have tried this:
df[df.groupby('subjects').value.apply(lambda x: x > 5)]
But it selects those having value greater than 5, and I want initial values to less than 5 and if there is a change in value and subsequent value is greater than 5, then return those subjects.