Amelio Vazquez-Reina
Amelio Vazquez-Reina

Reputation: 96264

Sequential within-group enumeration in Pandas

Say I have the following dataframe:

          date         A         B         C         D
0   2014-03-20   -1.561714  0.979202 -0.454935 -0.629215
1   2014-03-20    0.390851  0.045697 -1.683257 -0.771027
2   2014-03-20    0.357208 -0.207104  1.949936 -0.752096
3   2014-03-21    0.920687 -2.168321  0.337211 -0.638050
4   2014-03-21   -1.242688  0.541613  0.888365 -1.777855
5   2014-03-21   -0.425210 -1.876906 -0.805386  1.213882
6   2014-03-21    0.166220 -0.153913 -0.156115  0.155276
7   2014-03-22   -0.606291  0.574996 -0.139612  0.896219
8   2014-03-22    1.397973 -1.533777  1.319217 -0.292872
9   2014-03-22   -0.570876 -0.029911  2.143281 -0.515672

I would like to add a column that enumerates rows within each date group.

The above should become:

          date         A         B         C         D   date_indexer
0   2014-03-20 -1.561714  0.979202 -0.454935 -0.629215       0
1   2014-03-20  0.390851  0.045697 -1.683257 -0.771027       1
2   2014-03-20  0.357208 -0.207104  1.949936 -0.752096       2
3   2014-03-21  0.920687 -2.168321  0.337211 -0.638050       0
4   2014-03-21 -1.242688  0.541613  0.888365 -1.777855       1
5   2014-03-21 -0.425210 -1.876906 -0.805386  1.213882       2
6   2014-03-21  0.166220 -0.153913 -0.156115  0.155276       3
7   2014-03-22 -0.606291  0.574996 -0.139612  0.896219       0
8   2014-03-22  1.397973 -1.533777  1.319217 -0.292872       1
9   2014-03-22 -0.570876 -0.029911  2.143281 -0.515672       2

Any thoughts on how to do this in Pandas?

Upvotes: 3

Views: 307

Answers (1)

Dan Allan
Dan Allan

Reputation: 35235

df['date_indexer'] = df.groupby('date').cumcount()

N.B. This is a relatively new addition to pandas (v0.12 or 0.13, I think) so it will not work if you are running an old version.

Upvotes: 5

Related Questions