I trying to count the number of consecutive positive events for each column in a pandas dataframe. The solution provided by DSM here- Counting consecutive positive value in Python array works well for a given series.
import pandas as pd
a = [0,1,0,1,1,0,0,0,1,1,0,1,0]
b = [0,0,0,0,1,1,0,1,1,1,0,0,0]
series = pd.Series(a)
consecutiveCount(series).values
array([0, 1, 0, 1, 2, 0, 0, 0, 1, 2, 0, 1, 0], dtype=int64)
However, when I try to do this to a dataframe with several columns, I get the following.
df = pd.DataFrame({'a':a, 'b':b})
consecutiveCount(df)
ValueError: Grouper for '<class 'pandas.core.frame.DataFrame'>' not 1-dimensional
If I iterate though each column, it works but is very slow. Is there a vectorized way to process the entire dataframe at once?
Thanks!