Michael
Michael

Reputation: 351

Finding the streak of a value in pandas dataframe

I'm looking for some code that will generate the 'NewColumn' as below I basically want to find the streak of the current 'Result' value. For example if there have been 3 'Incorrect' results in a row then the new column should display the integer 3.

Thanks for any help

Day    Result        NewColumn
 1    Correct          1
 2    Correct          2
 3    Incorrect        1
 4    Incorrect        2
 5    Incorrect        3
 6    Incorrect        4
 7    Correct          1
 8    Correct          2

Upvotes: 1

Views: 353

Answers (2)

not_speshal
not_speshal

Reputation: 23166

This is a sort of well documented trick but I've adapted the logic from here into a one-liner:

df["NewColumn"] = df.groupby((df["Result"]!=df["Result"].shift()).cumsum()).cumcount()+1
>>> df
        Result  NewColumn
Day                      
1      Correct          1
2      Correct          2
3    Incorrect          1
4    Incorrect          2
5    Incorrect          3
6    Incorrect          4
7      Correct          1
8      Correct          2

Upvotes: 2

rhug123
rhug123

Reputation: 8778

Try this:

df.groupby(df['Result'].ne(df['Result'].shift()).cumsum()).cumcount()+1

Upvotes: 2

Related Questions