1

I have a data frame like this:

index   value
----    -----
1         A
2         A
3         A
4         A
5         B
6         B
7         A
8         B
9         C
10        C

I want to add a column to count continues occurrence of my value, like this:

index   value   continues-count
----    -----    ----------
1         A       1
2         A       2
3         A       3
4         A       4
5         B       1
6         B       2
7         A       1
8         B       1
9         C       1
10        C       2

I am able to do it using a loop but as my dataset is huge it takes forever!

Hamid
  • 612
  • 1
  • 8
  • 20

1 Answers1

3

It is just too boring to using shift and cumsum , let us try itertools

import itertools 
df['New']=list(itertools.chain(*[list(range(len(list(y))))for _,y in itertools.groupby(df.value)]))
df
Out[596]: 
   index value  New
0      1     A    0
1      2     A    1
2      3     A    2
3      4     A    3
4      5     B    0
5      6     B    1
6      7     A    0
7      8     B    0
8      9     C    0
9     10     C    1

pandas way

df['New']=df.groupby((df.value!=df.value.shift()).ne(0).cumsum()).cumcount()+1
BENY
  • 317,841
  • 20
  • 164
  • 234