Built-In Cumulative or Running Count?

1,759 views
Skip to first unread message

Daniel Kim

unread,
Feb 9, 2012, 8:32:52 PM2/9/12
to pystatsmodels
Hello,
Is there a built-in running count or cumulative count function in
pandas or numpy. I see there are cumsum functions available both in
pandas and in numpy, but I didn't see something that would get a
cumulative count. Let's say I have a dataframe like so:
df.groupby(['MODEL_YEAR','RO_YR_MTH'])['MILES_TO_FAIL'].count()

Output:
YR_MTH, COUNT
2011-03, 1
2011-04, 1
2011-06, 1
2011-07, 1
2011-08, 1
2011-10, 1
2011-12, 1

I want to create a column that contains a cumulative count like so:
YR_MTH, COUNT, CUM_COUNT
2011-03, 1, 1
2011-04, 1, 2
2011-06, 1, 3
2011-07, 1, 4
2011-08, 1, 5
2011-10, 1, 6
2011-12, 1, 7

Thanks in advance!

Wouter Overmeire

unread,
Feb 10, 2012, 3:26:47 AM2/10/12
to pystat...@googlegroups.com

If you want cumulative count, did you try cumsum on count in one go (did not try this one myself)?
df.groupby(['MODEL_YEAR','RO_YR_MTH'])['MILES_TO_FAIL'].count().cumsum()

In case of a DataFrame, and adding an extra column (but i suppose you know this):

In [35]: df
Out[35]:
    YR_MTH  COUNT
0  2011-03      1
1  2011-04      1
2  2011-06      1
3  2011-07      1
4  2011-08      1
5  2011-10      1
6  2011-12      1

In [36]: df['CUM_COUNT'] = df['COUNT'].cumsum()

In [37]: df
Out[37]:
    YR_MTH  COUNT  CUM_COUNT
0  2011-03      1          1
1  2011-04      1          2
2  2011-06      1          3
3  2011-07      1          4
4  2011-08      1          5
5  2011-10      1          6
6  2011-12      1          7




Daniel Kim

unread,
Feb 10, 2012, 7:05:27 AM2/10/12
to pystatsmodels
That was it. Didn't think to "double-up" on functions. Thanks!

On Feb 10, 3:26 am, Wouter Overmeire <loda...@gmail.com> wrote:
> 6  2011-12      1          7- Hide quoted text -
>
> - Show quoted text -
Reply all
Reply to author
Forward
0 new messages