6

I have a pandas groupby command which looks like this:

df.groupby(['year', 'month'], as_index=False).agg({'users':sum})

Is there a way I can name the agg output something other than 'users' during the groupby command? For example, what if I wanted the sum of users to be total_users? I could rename the column after the groupby is complete, but wonder if there is another way.

mikebmassey
  • 8,354
  • 26
  • 70
  • 95

2 Answers2

5

I like @Alexander answer, but there is also add_prefix:

df.groupby(['year','month']).agg({'users':sum}).add_prefix('total_')
bcollins
  • 3,379
  • 4
  • 19
  • 35
  • Is there a way to just rename only the `aggregated` columns? I think above code will also rename the `year` and `month` columns, if I am not mistaken. Appreciate your help. – Regressor Mar 03 '22 at 23:16
4

Per the docs:

If a dict is passed, the keys will be used to name the columns. Otherwise the function’s name (stored in the function object) will be used.

In [58]: grouped['D'].agg({'result1' : np.sum, ....:
'result2' : np.mean})

In your case:

df.groupby(['year', 'month'], as_index=False).users.agg({'total_users': np.sum})
Alexander
  • 105,104
  • 32
  • 201
  • 196
  • Thats a great answer! in this case **users** is the field you are aggregating on and **total_users** is new name of the field you are giving. – Gunay Anach Jul 12 '17 at 12:24