Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use groupby(...).agg(...) when you want a compact summary with one row per group; use groupby(...).transform(...) when each original row must remain and receive a group-level value. Aggregation changes the shape of the data, while transformation returns results aligned to the rows you started with.

Choose the GroupBy operation that matches your output

A pandas GroupBy operation splits data into groups, applies a calculation, and combines the results. The method you choose determines whether the result is shorter than the source, remains row-aligned, or selects some of the original records.

What you need Method Typical result
Summary values for each group agg, aggregate, or a built-in reduction such as sum() One row per group
A group statistic attached to every source row transform Same index as the grouped source object
Original rows selected using group conditions filter, head, tail, or nth A selection of source rows
Custom logic or output shape not covered by a more specific method apply Depends on what the function returns

For a standard summary, prefer agg or a built-in reduction. Reserve apply for cases where a more specific GroupBy method cannot express the operation; pandas notes that apply can be slower. See the pandas GroupBy reference and the documentation for GroupBy apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Summarize groups with agg

Aggregation reduces each group to one or more summary values. Unless you request otherwise, the group labels become the result index; grouping on multiple keys usually produces a MultiIndex. Use as_index=False to keep group labels as ordinary columns, or call reset_index() on the result.

This example summarizes points by team and counts non-missing player values. It also retains the record with a missing team by setting dropna=False:

import pandas as pd

df = pd.DataFrame({
    "team": ["A", "B", "A", None],
    "player": ["Mina", "Noah", "Omar", "Pia"],
    "points": [10, 7, 14, 8],
})

summary = (
    df.groupby("team", as_index=False, dropna=False)
      .agg(points_mean=("points", "mean"), player_count=("player", "count"))
)

The result has one row per team group, including the NA-key group. The aggregation output is intentionally shorter than the input when several records share a team. Named aggregation lets you choose clear output column names, as in points_mean and player_count. See the DataFrameGroupBy.agg reference.

Keep every row with transform

Use transform when a group calculation must be repeated or otherwise aligned to every source row. For example, the team mean can be added as a new column without reducing the DataFrame:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
df["team_mean"] = df.groupby("team", dropna=False)["points"].transform("mean")

df["team_mean"] has one value for each row of df, including the row whose team is missing. The aggregation above produces group summaries; this transformation preserves row alignment. That distinction matters when assigning results: do not assign a shorter aggregated Series to the full DataFrame as if its values still correspond one-for-one to the original rows. If you need to attach a separate summary, join it using explicit group keys and check the resulting index alignment.

The transform reference describes same-indexed results. In pandas 2.0, DataFrame-returning transforms changed to align their result index with the input index; the documentation notes that calling .to_numpy() avoids that alignment when raw positional behavior is intended.

Keep group labels out of the index

For DataFrame aggregation, set as_index=False in groupby when the group keys should remain regular columns. Alternatively, use the default indexed result and call reset_index():

summary = df.groupby("team").agg(points_mean=("points", "mean")).reset_index()

as_index controls aggregation output; it does not change transform‘s row alignment. If grouping by several columns, the default aggregation index contains the group keys as index levels, which reset_index() converts back into columns.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand missing keys and row order

Missing group keys

By default, groupby uses dropna=True, so rows with NA in a grouping key do not belong to a group and are omitted from grouped results. Set dropna=False when those rows should form an NA-key group. Choose deliberately: a grouped result with fewer records may reflect aggregation, excluded missing keys, or both.

Group order versus row order

By default, sort=True sorts group keys. Set sort=False to use the keys’ order of first appearance instead. This setting controls the order of groups, not the order of records inside each group: pandas preserves observation order within each group. See the pandas GroupBy sorting guide.

Categorical keys across pandas versions

When a grouping key is categorical, the observed option controls whether groups for unobserved category values appear. In pandas 3.0, observed defaults to True, so only observed categorical values are included. Older versions used a different default. Specify observed=True or observed=False explicitly when output must remain consistent across versions or include unobserved categories. The current defaults and version notes are listed in the DataFrame.groupby reference.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Use apply only for genuinely custom work

apply can combine a function’s DataFrame, Series, or scalar result across groups, making it flexible when the operation does not fit aggregation, transformation, or filtration. Its combined output shape depends on the function’s return value, so inspect the resulting index and columns rather than assuming they match the input. The group_keys option affects index construction for apply results; its default became True in pandas 2.0. The apply API reference documents these behaviors and cautions that specific methods such as agg and transform can be faster.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.