Q:

Pandas groupby and sum with the current month

This is the first 10 lines of the df that I am working with:

     id        user_id  session_date mb_used

0   1000_13     1000    2018-12-29  89.86
1   1000_204    1000    2018-12-31  0.00
2   1000_379    1000    2018-12-28  660.40
3   1000_413    1000    2018-12-26  270.99
4   1000_442    1000    2018-12-27  880.22
5   1001_0      1001    2018-08-24  284.68
6   1001_3      1001    2018-12-09  656.04
7   1001_4      1001    2018-11-04  16.97
8   1001_10     1001    2018-11-27  135.18
9   1001_15     1001    2018-12-13  761.92

My question is: how do I find the total amount of mb_used for each user_id per month? This means I would have to first isolate each user_id, find out how many rows did they use data in the same month, and then add those to get the 'data used per month' for each user. I can use pivot tables to find the total data used by each user using this code:
internet_per_user = pd.pivot_table(internet, index = 'user_id', columns='mb_used',aggfunc='sum')
But I am not able to incorporate the per month aspect.
For the 10 rows posted above, I would expect the output to look like this (hand-calculated):

user_id Month mb_used
1000    12    1901.47
1001    08    284.68
1001    12    1417.96
1001    11    152.15

A:

You will need to group on user ID for each month and perform a sum. You can use:
df['session_date'] = pd.to_datetime(df['session_date'], errors='coerce')
(df.groupby(['user_id', df['session_date'].dt.month])['mb_used']
   .sum()
   .reset_index())

   user_id  session_date  mb_used
0     1000            12  1901.47
1     1001             8   284.68
2     1001            11   152.15
3     1001            12  1417.96

