13

I'm using the pandas groupby+agg functionality to generate nice reports

aggs_dict = {'a':['mean', 'std'], 'b': 'size'}
df.groupby('year').agg(aggs_dict)

I would like to use the same aggs_dict on the entire dataframe as a single group, with no division to years, something like:

df.groupall().agg(aggs_dict)

or:

df.agg(aggs_dict)

But couldn't find any elegant way to do it.. Note that in my real code aggs_dict is quite complex so it's rather cumbersome to do:

df.a.mean()
df.a.std()
df.b.size()
....

am I missing something simple and nice?

ihadanny
  • 4,377
  • 7
  • 45
  • 76
  • @ayhan IIUC, it's the opposite - if the entire index would be one big duplicate, that would work here. The question is about an aggregation for the entire df as a group, not for each of the rows. – Ami Tavory Sep 07 '16 at 08:41
  • @AmiTavory Yeah I noticed that after your answer. :) – ayhan Sep 07 '16 at 08:41

3 Answers3

27

You could also use a function to directly group on:

 df.groupby(lambda x: True).agg(aggs_dict)
Hervé Mignot
  • 281
  • 3
  • 3
9

Ami Tavory's answer is a great way to do it but just in case you wanted a solution that doesn't require creating new columns and deleting them afterwards you could do something like:

df.groupby([True]*len(df)).agg(aggs_dict) 
bunji
  • 5,063
  • 1
  • 17
  • 36
6

You could add a dummy column:

df['dummy'] = 1

Then groupby + agg on it:

df.groupby('dummy').agg(aggs_dict)

and then delete it when you're done.

Community
  • 1
  • 1
Ami Tavory
  • 74,578
  • 11
  • 141
  • 185