Skip to Content
Numerical Computing with Python
book

Numerical Computing with Python

by Pratap Dangeti, Allen Yu, Claire Chung, Aldrin Yim
December 2018
Beginner to intermediate
682 pages
18h 1m
English
Packt Publishing
Content preview from Numerical Computing with Python

There's more...

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', ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering Numerical Computing with NumPy

Mastering Numerical Computing with NumPy

Umit Mert Cakmak, Tiago Antao, Mert Cuhadaroglu

Publisher Resources

ISBN: 9781789953633OtherOtherErrata Page