Question: I have a DataFrame with x and y coordinates, where the index represents a timestamp. We may assume it is an object that moves every timestep. The distance between consecutive timestamps is expected to increase. However, if the distance doesn't increase by a certain threshold, I consider it a potential "waiting" position. I use the word potential, because the data is quite noisy, and a single 'waiting' condition is not enough to be really sure that the object was not moving. Thus, I require at least 3 or more consecutive 'waiting' conditions, before I can be sure the object was indeed not moving. I would like to detect these waiting positions and label them accordingly in a new column. Example : x y timestamp 2023-07-01 00:00:00 1 5 2023-07-01 00:01:00 2 6 2023-07-01 00:02:00 3 7 2023-07-01 00:03:00 4 8 2023-07-01 00:04:00 4 8 2023-07-01 00:05:00 5 9 2023-07-01 00:06:00 6 9 2023-07-01 00:07:00 7 10 2023-07-01 00:08:00 7 10 2023-07-01 00:09:00 7 10 2023-07-01 00:10:00 7 10 2023-07-01 00:11:00 8 11 2023-07-01 00:12:00 9 11  To compute the distance, I already shifted the dataframe by 1, and caluclate the distance:  x y distance timestamp 2023-07-01 00:00:00 1 5 NaN 2023-07-01 00:01:00 2 6 1.414214 2023-07-01 00:02:00 3 7 1.414214 2023-07-01 00:03:00 4 8 1.414214 2023-07-01 00:04:00 4 8 0.000000 2023-07-01 00:05:00 5 9 1.414214 2023-07-01 00:06:00 6 9 1.000000 2023-07-01 00:07:00 7 10 1.414214 2023-07-01 00:08:00 7 10 0.000000 2023-07-01 00:09:00 7 10 0.000000 2023-07-01 00:10:00 7 10 0.000000 2023-07-01 00:11:00 8 11 1.414214 2023-07-01 00:12:00 9 11 1.000000  Now, assume if the distance is lower than 1, it could potentially be a waiting position:  x y distance condition_fulfilled timestamp 2023-07-01 00:00:00 1 5 NaN NaN 2023-07-01 00:01:00 2 6 1.414214 False 2023-07-01 00:02:00 3 7 1.414214 False 2023-07-01 00:03:00 4 8 1.414214 False 2023-07-01 00:04:00 4 8 0.000000 True 2023-07-01 00:05:00 5 9 1.414214 False 2023-07-01 00:06:00 6 9 1.000000 False 2023-07-01 00:07:00 7 10 1.414214 False 2023-07-01 00:08:00 7 10 0.000000 True 2023-07-01 00:09:00 7 10 0.000000 True 2023-07-01 00:10:00 7 10 0.000000 True 2023-07-01 00:11:00 8 11 1.414214 False 2023-07-01 00:12:00 9 11 1.000000 False  Since I require at least 3 consecutive fulfilled condtions, the expected output would be:  x y distance status timestamp 2023-07-01 00:00:00 1 5 NaN moving 2023-07-01 00:01:00 2 6 1.414214 moving 2023-07-01 00:02:00 3 7 1.414214 moving 2023-07-01 00:03:00 4 8 1.414214 moving 2023-07-01 00:04:00 4 8 0.000000 moving 2023-07-01 00:05:00 5 9 1.414214 moving 2023-07-01 00:06:00 6 9 1.000000 moving 2023-07-01 00:07:00 7 10 1.414214 moving 2023-07-01 00:08:00 7 10 0.000000 waiting 2023-07-01 00:09:00 7 10 0.000000 waiting 2023-07-01 00:10:00 7 10 0.000000 waiting 2023-07-01 00:11:00 8 11 1.414214 moving 2023-07-01 00:12:00 9 11 1.000000 moving 

Answer: Try: # fill the first NaN df['condition_fulfilled'] = df['condition_fulfilled'].bfill() tmp = (df['condition_fulfilled'] != df['condition_fulfilled'].shift()).cumsum() df['status'] = df.groupby(tmp)['condition_fulfilled'].transform(lambda x: 'waiting' if x.all() and len(x) >= 3 else 'moving') print(df)  Prints:  x y distance condition_fulfilled status timestamp 2023-07-01 00:00:00 1 5 NaN False moving 2023-07-01 00:01:00 2 6 1.414214 False moving 2023-07-01 00:02:00 3 7 1.414214 False moving 2023-07-01 00:03:00 4 8 1.414214 False moving 2023-07-01 00:04:00 4 8 0.000000 True moving 2023-07-01 00:05:00 5 9 1.414214 False moving 2023-07-01 00:06:00 6 9 1.000000 False moving 2023-07-01 00:07:00 7 10 1.414214 False moving 2023-07-01 00:08:00 7 10 0.000000 True waiting 2023-07-01 00:09:00 7 10 0.000000 True waiting 2023-07-01 00:10:00 7 10 0.000000 True waiting 2023-07-01 00:11:00 8 11 1.414214 False moving 2023-07-01 00:12:00 9 11 1.000000 False moving