pandas - aggregate count per month and calculate total sum of counts afterwards

Solution 1:

You can groupby twice, once by ['ID', 'Month'] and then by 'ID' to count per month per ID and total count per ID, respectively.

Note that in the first groupby is the case where you use count method but in the second groupby you use sum method because you're aggregating the counts.

out = df.groupby(['ID', 'Month']).agg(count=('ID','count')).reset_index()
out['total count'] = out.groupby('ID')['count'].transform('sum')

Output:

   ID  Month  count  total count
0   1    Feb      1            2
1   1  March      1            2
2   2    Feb      2            4
3   2  March      2            4
4   3    May      1            1