Examining the groupby object
The immediate result from using the groupby
method on a DataFrame will be a groupby object. Usually, we continue operating on this object to do aggregations or transformations without ever saving it to a variable. One of the primary purposes of examining this groupby object is to inspect individual groups.
Getting ready
In this recipe, we examine the groupby object itself by directly calling methods on it as well as iterating through each of its groups.
How to do it...
- Let's get started by grouping the state and religious affiliation columns from the college dataset, saving the result to a variable and confirming its type:
>>> college = pd.read_csv('data/college.csv') >>> grouped = college.groupby(['STABBR', 'RELAFFIL']) >>> type(grouped) pandas.core.groupby.DataFrameGroupBy
- Use the
dir
function to discover all its available functionality:
>>> print([attr for attr in dir(grouped) if not attr.startswith('_')]) ['CITY', 'CURROPER',...