.groupby()
StevenSwiniarski474 total contributions
Published Jun 11, 2022
Contribute to Docs
The .groupby()
function groups a DataFrame
using a mapper or a series of columns and returns a GroupBy
object. A range of methods, as well as custom functions, can be applied to GroupBy
objects in order to combine or transform large amounts of data in these groups.
Syntax
dataframevalue.groupby(by, axis, level, as_index, sort, group_keys, observed, dropna)
.groupby()
uses the following parameters:
by
: If a dictionary orSeries
is passed, the values will determine groups. If a list or ndarray with the same length as the selected axis is passed, the values will be used to form groups. A label or list of labels can be used to group by a particular column or columns.axis
: Split along rows (0 or “index”) or columns (1 or “columns”). Default value is 0.level
: If the axis is aMultiIndex
, group by a particular level or levels. Value is int or level name, or sequence of them. Default value isNone
.as_index
: Boolean value.True
returns group labels as an index in aggregated output, andFalse
returns labels asDataFrame
columns. Default value isTrue
.sort
: Boolean value.True
sorts the group keys. Default value isTrue
.group_keys
: Boolean value. Add group keys to index when calling apply. Default value isTrue
.observed
: Boolean value. IfTrue
, only show observed values for categorical groupers, otherwise show all values. Default value isFalse
.dropna
: Boolean value. IfTrue
, drop groups whose keys containNA
values. IfFalse
,NA
will be used as a key for those groups. Default value isTrue
.
Example
This example uses .groupby()
on a DataFrame
to produce some aggregate results.
import pandas as pddf = pd.DataFrame({'Key' : ['A', 'A', 'A', 'B', 'B', 'C'],'Value' : [15., 23., 17., 5., 8., 12.]})print(df, end='\n\n')print(df.groupby(['Key'], as_index=False).mean(), end='\n\n')print(df.groupby(['Key'], as_index=False).sum())
This produces the following output:
Key Value0 A 15.01 A 23.02 A 17.03 B 5.04 B 8.05 C 12.0Key Value0 A 18.3333331 B 6.5000002 C 12.000000Key Value0 A 55.01 B 13.02 C 12.0
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.