- DataFrame.groupby(by=None, axis=_NoDefault.no_default, level=None, as_index=True, sort=True, group_keys=True, observed=_NoDefault.no_default, dropna=True)[source]#
Group DataFrame using a mapper or by a Series of columns.
A groupby operation involves some combination of splitting theobject, applying a function, and combining the results. This can beused to group large amounts of data and compute operations on thesegroups.
- Parameters:
- bymapping, function, label, pd.Grouper or list of such
Used to determine the groups for the groupby.If
by
is a function, it’s called on each value of the object’sindex. If a dict or Series is passed, the Series or dict VALUESwill be used to determine the groups (the Series’ values are firstaligned; see.align()
method). If a list or ndarray of lengthequal to the selected axis is passed (see the groupby user guide),the values are used as-is to determine the groups. A label or listof labels may be passed to group by the columns inself
.Notice that a tuple is interpreted as a (single) key.- axis{0 or ‘index’, 1 or ‘columns’}, default 0
Split along rows (0) or columns (1). For Series this parameteris unused and defaults to 0.
Deprecated since version 2.1.0: Will be removed and behave like axis=0 in a future version.For
axis=1
, doframe.T.groupby(...)
instead.- levelint, level name, or sequence of such, default None
If the axis is a MultiIndex (hierarchical), group by a particularlevel or levels. Do not specify both
by
andlevel
.- as_indexbool, default True
Return object with group labels as theindex. Only relevant for DataFrame input. as_index=False iseffectively “SQL-style” grouped output. This argument has no effecton filtrations (see the filtrations in the user guide),such as
head()
,tail()
,nth()
and in transformations(see the transformations in the user guide).- sortbool, default True
Sort group keys. Get better performance by turning this off.Note this does not influence the order of observations within eachgroup. Groupby preserves the order of rows within each group. If False,the groups will appear in the same order as they did in the original DataFrame.This argument has no effect on filtrations (see the filtrations in the user guide),such as
head()
,tail()
,nth()
and in transformations(see the transformations in the user guide).Changed in version 2.0.0: Specifying
sort=False
with an ordered categorical grouper will nolonger sort the values.- group_keysbool, default True
When calling apply and the
by
argument produces a like-indexed(i.e. a transform) result, add group keys toindex to identify pieces. By default group keys are not includedwhen the result’s index (and column) labels match the inputs, andare included otherwise.Changed in version 1.5.0: Warns that
group_keys
will no longer be ignored when theresult fromapply
is a like-indexed Series or DataFrame.Specifygroup_keys
explicitly to include the group keys ornot.Changed in version 2.0.0:
group_keys
now defaults toTrue
.See AlsoDropNA — sktime documentation- observedbool, default False
This only applies if any of the groupers are Categoricals.If True: only show observed values for categorical groupers.If False: show all values for categorical groupers.
Deprecated since version 2.1.0: The default value will change to True in a future version of pandas.
- dropnabool, default True
If True, and if group keys contain NA values, NA values togetherwith row/column will be dropped.If False, NA values will also be treated as the key in groups.
- Returns:
- pandas.api.typing.DataFrameGroupBy
Returns a groupby object that contains information about the groups.
See also
- resample
Convenience method for frequency conversion and resampling of time series.
Notes
See the user guide for moredetailed usage and examples, including splitting an object into groups,iterating through groups, selecting a group, aggregation, and more.
Examples
>>> df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',... 'Parrot', 'Parrot'],... 'Max Speed': [380., 370., 24., 26.]})>>> df Animal Max Speed0 Falcon 380.01 Falcon 370.02 Parrot 24.03 Parrot 26.0>>> df.groupby(['Animal']).mean() Max SpeedAnimalFalcon 375.0Parrot 25.0
Hierarchical Indexes
We can groupby different levels of a hierarchical indexusing the level parameter:
>>> arrays = [['Falcon', 'Falcon', 'Parrot', 'Parrot'],... ['Captive', 'Wild', 'Captive', 'Wild']]>>> index = pd.MultiIndex.from_arrays(arrays, names=('Animal', 'Type'))>>> df = pd.DataFrame({'Max Speed': [390., 350., 30., 20.]},... index=index)>>> df Max SpeedAnimal TypeFalcon Captive 390.0 Wild 350.0Parrot Captive 30.0 Wild 20.0>>> df.groupby(level=0).mean() Max SpeedAnimalFalcon 370.0Parrot 25.0>>> df.groupby(level="Type").mean() Max SpeedTypeCaptive 210.0Wild 185.0
We can also choose to include NA in group keys or not by settingdropna parameter, the default setting is True.
>>> l = [[1, 2, 3], [1, None, 4], [2, 1, 3], [1, 2, 2]]>>> df = pd.DataFrame(l, columns=["a", "b", "c"])
>>> df.groupby(by=["b"]).sum() a cb1.0 2 32.0 2 5
>>> df.groupby(by=["b"], dropna=False).sum() a cb1.0 2 32.0 2 5NaN 1 4
>>> l = [["a", 12, 12], [None, 12.3, 33.], ["b", 12.3, 123], ["a", 1, 1]]>>> df = pd.DataFrame(l, columns=["a", "b", "c"])
>>> df.groupby(by="a").sum() b caa 13.0 13.0b 12.3 123.0
>>> df.groupby(by="a", dropna=False).sum() b caa 13.0 13.0b 12.3 123.0NaN 12.3 33.0
When using
.apply()
, usegroup_keys
to include or exclude thegroup keys. Thegroup_keys
argument defaults toTrue
(include).>>> df = pd.DataFrame({'Animal': ['Falcon', 'Falcon',... 'Parrot', 'Parrot'],... 'Max Speed': [380., 370., 24., 26.]})>>> df.groupby("Animal", group_keys=True)[['Max Speed']].apply(lambda x: x) Max SpeedAnimalFalcon 0 380.0 1 370.0Parrot 2 24.0 3 26.0
>>> df.groupby("Animal", group_keys=False)[['Max Speed']].apply(lambda x: x) Max Speed0 380.01 370.02 24.03 26.0
FAQs
Pandas.DataFrame.groupby — pandas 2.2.2 documentation? ›
Group DataFrame using a mapper or by a Series of columns. A groupby operation involves some combination of splitting the object, applying a function, and combining the results. This can be used to group large amounts of data and compute operations on these groups.
How does the groupby() method work in pandas? ›Splitting the Original Object into Groups
At this stage, we call the pandas DataFrame. groupby() function. We use it to split the data into groups based on predefined criteria, along rows (by default, axis=0 ), or columns ( axis=1 ). In other words, this function maps the labels to the names of the groups.
- Split: The different groups are split with their values.
- Apply: The aggregate function is applied to the values of these groups.
- Combine: The values are combined in a single row.
To convert a DataFrameGroupBy object to a regular DataFrame object, you can use the reset_index function. This function resets the index of the DataFrame and returns a new DataFrame object. As you can see, the df_new object is a regular DataFrame object that contains the grouped data.
How do you aggregate data using the groupby() method in pandas? ›The Hello, World! of pandas GroupBy
You call . groupby() and pass the name of the column that you want to group on, which is "state" . Then, you use ["last_name"] to specify the columns on which you want to perform the actual aggregation. You can pass a lot more than just a single column name to .
Pandas provides the groupby() method to group data based on one or more columns. Once the data is grouped, we can apply various aggregation functions such as sum() , mean() , max() , min() , count() , etc. to calculate statistics for each group. This will group the data by Product and Region columns.
What is the output of a groupby() method of a DataFrame? ›The resulting output of a groupby() operation can be a pandas Series or dataframe, depending on the operation and data structure.
How do you group data in a data frame? ›To group a Pandas DataFrame based on specific column values, use the groupby() function with that column as an argument.
What is the difference between groupby and pivot in Python? ›Groupby is ideal for applying multiple aggregations and performing complex data manipulations on grouped data. Pivot Table is excellent for creating summarized views of data with a clear structure, often used for reporting and data analysis.
What does pandas groupby return? ›Returns: pandas.api.typing.DataFrameGroupBy. Returns a groupby object that contains information about the groups. Convenience method for frequency conversion and resampling of time series.
How do I keep all columns after groupby Pandas? ›
If you want to keep all of the original columns in the grouped dataframe, you can use the transform() function instead of agg() . transform() applies a function to each group and returns a dataframe with the same shape as the original dataframe.
How do you select a group in Pandas groupby? ›Selecting a groups
In order to select a group, we can select group using GroupBy. get_group(). We can select a group by applying a function GroupBy. get_group this function select a single group.
HAVING filtering_condition; The aggregate function is written in the SELECT statement, and the function's result will be shown as an additional column. The GROUP BY clause contains the columns by which you want your output to be grouped. This clause is often used with the WHERE and HAVING clauses for filtering.
How does groupby works? ›The GROUP BY Statement in SQL is used to arrange identical data into groups with the help of some functions. i.e. if a particular column has the same values in different rows then it will arrange these rows in a group.
What does the group method do in Python? ›Method/Attribute | Purpose |
---|---|
group() | Return the string matched by the RE |
start() | Return the starting position of the match |
end() | Return the ending position of the match |
span() | Return a tuple containing the (start, end) positions of the match |
first. Compute the first entry of each column within each group. Defaults to skipping NA elements.
What does groupby count do in pandas? ›The . groupby() function of pandas is used to group similar data and helps to perform operations on the grouped data. The pandas groupby count function of python is used to count the number of times a value appears in the data.