December 2018
Beginner to intermediate
682 pages
18h 1m
English
It is possible to replicate much more complex pivot tables with groupby aggregations. For instance, take the following result from pivot_table:
>>> flights.pivot_table(index=['AIRLINE', 'MONTH'], columns=['ORG_AIR', 'CANCELLED'], values=['DEP_DELAY', 'DIST'], aggfunc=[np.sum, np.mean], fill_value=0)

To replicate this with a groupby aggregation, simply follow the same pattern from the recipe and place all the columns from the index and columns parameters into the groupby method and then unstack the columns:
>>> flights.groupby(['AIRLINE', 'MONTH', 'ORG_AIR', 'CANCELLED']) \ ['DEP_DELAY', 'DIST'] \ .agg(['mean', 'sum']) \ .unstack(['ORG_AIR', ...