Skip to main content

groupby

df.groupby(column_name) or df.groupby([column_names])

Groups all DataFrame rows with the same value in column_name or list of column_names.

A groupby operation groups large amounts of data based on the column name(s).

Input:
column_name : string
Groups by the column specified. The column becomes the index.
column_names : list (of strings)
Groups by all listed columns, starting with the first one in the list. The columns become the indices.
Returns:
A new DataFrame with the parameter column(s) as the index and all other columns grouped.
Return Type:
DataFrame
Clarification (groupby / pandas 2.0):
Pandas 2.0+ no longer silently drops columns that can’t be aggregated after a groupby. We must use .get() to select the column(s) we want before .groupby(...).mean() (or other aggregations) so that our code runs properly on current pandas.
Note:
A groupby() is usually followed by an aggregate method. A groupby() without an aggregate method will return a DataFrameGroupBy object rather than a DataFrame.

Aggregate Methods
.mean()   .median()   .count()   .max()   .min()   .sum()

The diagram below provides a visualization of how groupby works using a variation of our main dataset. For additional helpful visual guides, please visit the Diagrams site.


pets
IndexIDSpeciesColorWeightAgeIs_CatOwner_Comment
0dog_001dogblack405False There are no bad dogs, only bad owners.
1cat_001catgolden1.50.2TrueMy best birthday present ever!!!
2cat_002catblack159True****All you need is love and a cat.****
3dog_002dogwhite802FalseLove is a wet nose and a wagging tail.
4dog_003dogblack250.5FalseBe the person your dog thinks you are.
5ham_001hamsterblack13FalseNo, thank you!
6ham_002hamstergolden0.250.2FalseNo, thank you!
7cat_003catblack100TrueNo, thank you!

.groupby() with one column

pets.groupby('Species').count()
IndexIDColorWeightAgeIs_CatOwner_Comment
cat333333
dog333333
hamster222222

.groupby() with multiple columns

pets.groupby(['Species', 'Color']).count().reset_index()
IndexSpeciesColorIDWeightAgeIs_CatOwner_Comment
0catblack22222
1catgolden11111
2dogblack22222
3dogwhite11111
4hamsterblack11111
5hamstergolden11111

See Also

Problems or suggestions about this page? Fill out our feedback form.