I'm getting some timestamped data (shown below) through an API, and I want to check starting from the most recent entry (which in this case is the last row) how long a certain column value been consecutively greater than a certain threshold number. Here is some sample data that I converted to a df:
ID Timestamp Value
0 20779453 2021-01-18 09:15:00Z 62.47612
1 20779453 2021-01-18 09:20:00Z 54.56400
2 20779453 2021-01-18 09:25:00Z 64.95384
3 20779453 2021-01-18 09:30:00Z 63.62500
4 20779453 2021-01-18 09:35:00Z 61.51790
In this case, I want to check how long the Value variable has been greater than 60 (the answer here is 3 times ie 15 mins). I'm not strong with python, and my instinct is to use a for loop to iterate through the df and keep a counter, but I know that using loops with dfs is not a good practice. Here I've created a sample df
data = [[20779453, '2021-01-18 09:15:00Z', 62.47612], [20779453, '2021-01-18 09:20:00Z', 54.56400],
[20779453, '2021-01-18 09:25:00Z', 64.95384], [20779453, '2021-01-18 09:30:00Z', 63.62500],
[20779453, '2021-01-18 09:35:00Z', 61.51790]]
df = pd.DataFrame(data, columns = ['ID', 'Timestamp', 'Value'])
count = df[df['Value']>60]['Value'].count()
count
Output based on above code: 4
Expected output: 3 (since the latest timestamp, the number of times the value has consecutively been greater than 60 is 3)
EDIT: Another test case based on Akshay's answer:
data = [[20779453, '2021-01-18 09:15:00Z', 62.47612], [20779453, '2021-01-18 09:20:00Z', 54.56400],
[20779453, '2021-01-18 09:25:00Z', 64.95384], [20779453, '2021-01-18 09:30:00Z', 63.62500],
[20779453, '2021-01-18 09:35:00Z', 61.51790], [20779453, '2021-01-18 09:40:00Z', 63.62500],
[20779453, '2021-01-18 09:40:00Z', 53.62500],[20779453, '2021-01-18 09:45:00Z', 61.51790]]
df1 = pd.DataFrame(data, columns = ['ID', 'Timestamp', 'Value'])
from itertools import groupby
max([len(list(g)) for k, g in groupby(df['Value']>60) if k==True])
Expected output: 1
Current output: 4
I think what I didn't get across well is that I only care about the latest subsequent value and not the longest. Any ideas?