2

What is the best way to use pandas.pivot_table to calculate aggregated functions over the whole table without providing the grouping?

For example, if I want to calculate the sum of A,B,C into one table with a single row without grouping by any of the columsn:

>>> x = pd.DataFrame({'A':[1,2,3],'B':[8,7,6],'C':[0,3,2]})
>>> x
   A  B  C
0  1  8  0
1  2  7  3
2  3  6  2
>>> x.pivot_table(values=['A','B','C'],aggfunc=np.sum)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/tool/pandora64/.package/python-2.7.5/lib/python2.7/site-packages/pandas/tools/pivot.py", line 103, in pivot_table
    grouped = data.groupby(keys)
  File "/tool/pandora64/.package/python-2.7.5/lib/python2.7/site-packages/pandas/core/generic.py", line 2434, in groupby
    sort=sort, group_keys=group_keys, squeeze=squeeze)
  File "/tool/pandora64/.package/python-2.7.5/lib/python2.7/site-packages/pandas/core/groupby.py", line 789, in groupby
    return klass(obj, by, **kwds)
  File "/tool/pandora64/.package/python-2.7.5/lib/python2.7/site-packages/pandas/core/groupby.py", line 238, in __init__
    level=level, sort=sort)
  File "/tool/pandora64/.package/python-2.7.5/lib/python2.7/site-packages/pandas/core/groupby.py", line 1622, in _get_grouper
    raise ValueError('No group keys passed!')
ValueError: No group keys passed!

Also, I would like to use custom aggfunc, and the above np.sum is just an example.

Thanks.

Pan Yan
  • 1,622
  • 1
  • 18
  • 21
  • 4
    Sorry what's wrong with `df.sum()` here? – EdChum Jul 01 '15 at 18:55
  • Good question. If that's not want you want, try posting desired results. – JohnE Jul 01 '15 at 20:37
  • Thanks EdChum and JohnE. The thing is I want to be able to use custom aggfunc as well. np.sum is just an example. What if I also want to write my own aggfunc? pivot_table seems do what I need but just requires at least one group function. – Pan Yan Jul 06 '15 at 00:52

2 Answers2

3

I think you're asking how to apply a function to all columns of a Data Frame: To do this call the apply method of your dataframe:

def myfunc(col):
   return np.sum(col)

x.apply(myfunc)

Out[1]: 
A     6
B    21
C     5
dtype: int64
maxymoo
  • 35,286
  • 11
  • 92
  • 119
1

I had the same error, I was using pivot_table argument on a Pandas data frame,

import numpy as np
# Pivot for mean weekly_sales for each store type
mean_sales_by_type = sales.pivot_table(values='weekly_sales')

# Print mean_sales_by_type
print(mean_sales_by_type)

Here's the error:

File "<stdin>", line 889, in __init__
grouper, exclusions, obj = get_grouper(
File "<stdin>", line 896, in get_grouper
raise ValueError("No group keys passed!")
ValueError: No group keys passed!

Finally got it fixed it by specifying the index argument of the pivot_table function (after values)

mean_sales_by_type = sales.pivot_table(values='weekly_sales',index='type')

in your case try this:-

x.pivot_table(values=['A','B','C'],**value=[]**,aggfunc=np.sum)
S.B
  • 13,077
  • 10
  • 22
  • 49
Mboya
  • 11
  • 2