creating a column which keeps a running count of consecutive values

You can use the compare-cumsum-groupby pattern (which I really need to getting around to writing up for the documentation), with a final cumcount: >>> df = pd.DataFrame({“binary”: [0,1,1,1,0,0,1,1,0]}) >>> df[“consec”] = df[“binary”].groupby((df[“binary”] == 0).cumsum()).cumcount() >>> df binary consec 0 0 0 1 1 1 2 1 2 3 1 3 4 0 0 5 0 … Read more