'How to find the next zero that follows a specified value and pull out its time stamp?

I have a df that looks something like this but longer:

df = pd.DataFrame({
    'Time' : [1,2,7,10,15,16,77,98,999,1000,1121,1245,1373,1490,1555],  
    'ID' : ['1', '1', '1', '1', '1', '2', '2', '2', '2', '2', '3', '3', '3', '3', '3'],
    'Act' : ['1', '2', '4', '1', '2', '0', '2', '4', '3', '1', '4', '0', '3', '1', '2']})

The Act values range from 0-4. I would like to find the first zero following a 4 and then take the "Time" column value for that zero. If there are two 4s in a row and only then a zero, I am only interested in the 4 closest to the zero.

For the above example I would like the values: 16 and 1245 and a append them to a vector.

Thank you!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source