How to do it...

  1. Read in the flights dataset, and answer the first query by defining the grouping columns (AIRLINE, WEEKDAY), the aggregating column (CANCELLED), and the aggregating function (sum):
>>> flights.groupby(['AIRLINE', 'WEEKDAY'])['CANCELLED'] 
.agg('sum').head(7)
AIRLINE WEEKDAY AA 1 41 2 9 3 16 4 20 5 18 6 21 7 29 Name: CANCELLED, dtype: int64
  1. Answer the second query by using a list for each pair of grouping and aggregating columns. Also, use a list for the aggregating functions:
>>> flights.groupby(['AIRLINE', 'WEEKDAY']) 
['CANCELLED', 'DIVERTED'].agg(['sum', 'mean']).head(7)
  1. Answer the third query using a dictionary in the agg method to map specific aggregating columns to specific aggregating functions:
>>> group_cols = ['ORG_AIR', 'DEST_AIR']
>>> agg_dict = {'CANCELLED':['sum', 'mean', 'size'],
'AIR_TIME':['mean', 'var']}
>>> flights.groupby(group_cols).agg(agg_dict).head()
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset