I have the following dataframe: df = pd.DataFrame({"id": ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C','C','C','C'], "date": [2015, 2016, 2017, 2018, 2015, 2016, 2017, 2018, 2015, 2016, 2017, 2018], "col_1": [1,1,1,0,1,0,1,1,0,1,1,1]}) I want to add a new col that should be a dummy 1 for a group if "col_1" has three consecutive 1s. The resulting data-frame should be: df_new = pd.DataFrame({"id": ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C','C','C','C' ], "date": [2015, 2016, 2017, 2018, 2015, 2016, 2017, 2018, 2015, 2016, 2017, 2018], "col_1": [1,1,1,0,1,0,1,1,0,1,1,1], "col_2": [1,1,1,1,0,0,0,0,1,1,1,1]}) Continue reading...