'Keep maximum row in consecutive rows with specific value in column based on another column value

I have a df.

import pandas as pd 
df = pd.DataFrame({'id_c':[1] * 4 + [2] * 3 + [3] * 4,
        'Run':[7,8,5,4,3,2,1,2,3,4,5], 
      'Date_diff':[4,12,0,0,2,2,10,1,1,3,3]})

id_c   Run   Date_diff

1    7      4
1    8      12
1    5      0
1    4      0
2    3      2
2    2      2
2    1      10
3    2      1
3    3      1
3    4      3
3    5      3

For each unique value of id_c , if Date_diff equals to 0 , 1 , 2 for two consecutive rows , I want to keep the row with the maximum value in Run.

I tried :

df.groupby(['id_c' , 'Date_diff'])['Run'].idxmax()]

But it also selects maximum values for values of Date_diff different than 0 , 1 , 2.

The desired output would be :

id_c Run   Date_diff
1    7      4
1    8      12
1    5      0
2    3      2
2    1      10
3    3      1
3    4      3
3    5      3

Thanks!



Sources

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

Source: Stack Overflow

Solution Source