'pandas pivot_table without grouping

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.



Solution 1:[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)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 S.B