Add a sequence number to each element in a group using python

I stumbled upon the answer which was embarrassingly simple. The groupby statement has a 'cumcount()' option which will enumerate group items.

df['sequence']=df.groupby('patient').cumcount()

The caveat is that the records have to be in the order you want them enumerated.


Firstly you want to convert the date column to be a pandas datetime (rather than strings):

In [11]: pd.to_datetime(df['date'], format='%d%b%Y')
Out[11]:
0   2009-06-20
1   2009-06-24
2   2009-07-15
3   2008-02-09
4   2008-02-21
5   2010-03-14
6   2010-05-02
7   2010-05-12
Name: date, dtype: datetime64[ns]

Note: see docs for possible format options.

In [12]: df['date'] = pd.to_datetime(df['date'], format='%d%b%Y')

In [13]: df
Out[13]:
   patient       date  sequence
0      145 2009-06-20         1
1      145 2009-06-24         2
2      145 2009-07-15         3
3      582 2008-02-09         1
4      582 2008-02-21         2
5      987 2010-03-14         1
6      987 2010-05-02         2
7      987 2010-05-12         3

If this isn't in date order (for each patient), I would sort it first:

In [14]: df = df.sort('date')

Now you can groupby and cumcount:

In [15]: g = df.groupby('patient')

In [16]: g.cumcount() + 1
Out[16]:
2    1
3    2
0    1
1    2
4    1
5    2
6    3
dtype: int64

Which is what you want (althout it's out of order):

In [17]: df['sequence'] = g.cumcount() + 1

In [18]: df
Out[18]:
       patient       date  sequence
2      582 2008-02-09         1
3      582 2008-02-21         2
0      145 2009-06-24         1
1      145 2009-07-15         2
4      987 2010-03-14         1
5      987 2010-05-02         2
6      987 2010-05-12         3

To rearrange (though you may not need to) use sort_index (or we could reindex if we saved the initial DataFrame's index):*

In [19]: df.sort_index()
Out[19]:
   patient       date  sequence
0      145 2009-06-24         1
1      145 2009-07-15         2
2      582 2008-02-09         1
3      582 2008-02-21         2
4      987 2010-03-14         1
5      987 2010-05-02         2
6      987 2010-05-12         3