Examining the groupby object
The immediate result from using the .groupby method on a DataFrame is a groupby object. Usually, we chain operations on this object to do aggregations or transformations without ever storing the intermediate values in variables.
In this recipe, we examine the groupby object to examine individual 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) <class 'pandas.core.groupby.generic.DataFrameGroupBy'> - Use the
dirfunction to discover the attributes of agroupbyobject:>>> print([attr for attr in dir(grouped) if not ... attr.startswith('_')]) ['CITY&apos...