Let's say we have the following pandas DataFrame:
In [1]: import pandas as pd import numpy as np df = pd.DataFrame([0, 1, 0, 0, 1, 1, 0, 1, 1, 1], columns=['in']) df Out[1]: in 0 0 1 1 2 0 3 0 4 1 5 1 6 0 7 1 8 1 9 1
How to count the number of consecutive in vector form in pandas? I would like to get this result:
in out 0 0 0 1 1 1 2 0 0 3 0 0 4 1 1 5 1 2 6 0 0 7 1 1 8 1 2 9 1 3
Something like the cumsum vectorization operation, which is reset in a certain state.
python vectorization pandas
Puggie
source share