There's more...

It is possible to apply our customized function to multiple aggregating columns. We simply add more column names to the indexing operator. The max_deviation function only works with numeric columns:

>>> college.groupby('STABBR')['UGDS', 'SATVRMID', 'SATMTMID'] 
.agg(max_deviation).round(1).head()

You can also use your customized aggregation function along with the prebuilt functions. The following does this and groups by state and religious affiliation:

>>> college.groupby(['STABBR', 'RELAFFIL']) 
['UGDS', 'SATVRMID', 'SATMTMID']
.agg([max_deviation, 'mean', 'std']).round(1).head()

Notice that pandas uses the name of the function as the name for the returned column. You can change the column name directly with the rename method or you can modify the special function attribute __name__:

>>> max_deviation.__name__
'max_deviation'

>>> max_deviation.__name__ = 'Max Deviation'
>>> college.groupby(['STABBR', 'RELAFFIL'])
['UGDS', 'SATVRMID', 'SATMTMID']
.agg([max_deviation, 'mean', 'std']).round(1).head()
..................Content has been hidden....................

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