0

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
Michael
  • 343
  • 2
  • 7

2 Answers2

2

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
not_speshal
  • 22,093
  • 2
  • 15
  • 30
1

Try this:

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

rhug123
  • 7,893
  • 1
  • 9
  • 24