Free preview

117 lessons

Essential Python for Data Science and ML

Free preview

Essential Python for Data Science and ML · 117 lessons

Learn Python properly - from basics to ML-ready

No surprise gaps

Actually remember it

Skip what you know

One subscription. All learning paths included.

Our content is best on a larger screen

Pandas aggregations and groupby operations

Simple aggregations on DataFrames and Series

Explanation

To extract meaningful insights from raw data, we need to summarise it - calculating totals, averages, counts, and other statistics that reveal patterns. This process is called aggregation.

Pandas aggregation methods operate column-wise by default. When applied to a DataFrame, they produce a Series where each value summarises one column. When applied to a single Series, they return a scalar value.

The most commonly used aggregation methods:

  • .sum() - column totals
  • .mean() - arithmetic average
  • .count() - number of non-missing values
  • .min() / .max() - smallest / largest values
  • .std() / .var() - standard deviation / variance

These methods automatically skip missing values (NaN) and only work on columns with compatible data types.

Warning

Applying an aggregation to an incompatible data type (e.g. calculating .mean() on a text column) will raise an error or return unexpected results.

Example

Let's work with a sales dataset to see aggregation in action.

Dataset creation

Details

    product month  units_sold  revenue  profit_margin
0  Widget A   Jan         120   2400.0           0.25
1  Widget B   Jan          85   1275.0           0.30
2  Gadget X   Jan         200   6000.0           0.15
3  Widget A   Feb         135   2700.0           0.28
4  Gadget X   Feb         180   5400.0           0.18
5  Widget B   Feb          92   1380.0           0.32

Aggregating a DataFrame

Calling .sum() on the numeric columns produces a Series whose labels are the column names:

numeric_columns = ["units_sold", "revenue", "profit_margin"]
print(df[numeric_columns].sum())
units_sold         812.00
revenue          19155.00
profit_margin        1.48
dtype: float64

Aggregating a Series

Applying an aggregation to a single Series returns a scalar value:

print(df["units_sold"].sum())  # np.int64(812)
print(df["product"].count())  # np.int64(6)

Practice questions

3 questions

A DataFrame containing the Titanic dataset has been loaded into the Python Editor.

Calculate the fraction of non-missing values for each column by dividing the count of non-missing entries by the total number of rows.

Assign the resulting Series to a variable named missing_data.

Once complete, run the code to reveal a token used to verify your answer. What is the token?

Select the correct answer:

+ 2 more questions

Using .groupby() operations with single groups

Explanation

While overall summaries are useful, the real analytical power comes from calculating statistics by group - for example, sales by product, by month, or by region. This is where .groupby() becomes essential.

The .groupby() method implements the split-apply-combine pattern:

  1. Split - the DataFrame is divided into groups based on the values in one or more columns.
  2. Apply - an aggregation function (like .sum() or .mean()) is applied to each group separately.
  3. Combine - the results from all groups are assembled into a new DataFrame or Series.

The syntax follows this pattern:

df.groupby("grouping_column")["columns_to_aggregate"].aggregation_method()

The grouping column becomes the index of the result. We typically select which columns to aggregate, preventing Pandas from attempting meaningless operations like summing text columns.

What happens during .groupby()?

Details

Example

Let's use a sales dataset to explore grouping operations.

Dataset creation

Details

Output

Details


Grouping with multiple aggregated columns

Grouping by product and summing the numeric columns produces a DataFrame:

numeric_columns = ["units_sold", "revenue"]
product_totals = df.groupby("product")[numeric_columns].sum()
print(product_totals)
          units_sold  revenue
product
Gadget X         575    17250
Widget A         365     7300
Widget B         265     3975

The grouping column ("product") becomes the index of the result. This breakdown reveals that Gadget X has the highest total sales across all regions and months.


Grouping with a single aggregated column

Aggregating only one column produces a Series instead:

print(df.groupby("product")["units_sold"].mean())
product
Gadget X    191.666667
Widget A    121.666667
Widget B     88.333333
Name: units_sold, dtype: float64

Practice questions

3 questions

A DataFrame containing the Tips dataset has been loaded into the Python Editor.

What is the average total bill on a Friday (to 2 decimal places)?

Select the correct answer:

+ 2 more questions

Multi-column grouping

Explanation

We can group by multiple columns simultaneously by passing a list. This allows for analysis like "revenue by product and month" or "survival rate by class and sex".

The syntax extends naturally from single-column grouping:

df.groupby(["col_1", "col_2"])["cols_to_aggregate"].aggregation_method()

When we group by multiple columns, Pandas creates a multi-level index where each unique combination of grouping values identifies a row in the result.

What is a multi-level index?

Details

Example

Using the same sales dataset, we can group by both region and product simultaneously.

Dataset creation

Details

Passing a list of columns to .groupby() creates a multi-level breakdown:

numeric_columns = ["units_sold", "revenue"]
region_product_summary = df.groupby(["region", "product"])[numeric_columns].sum()
print(region_product_summary)
                 units_sold  revenue
region product
North  Gadget X         180     5400
       Widget A         230     4600
       Widget B         173     2595
South  Gadget X         395    11850
       Widget A         135     2700
       Widget B          92     1380

The result has a multi-level index with one level per grouping column. This breakdown reveals that Gadget X performs much better in the South, while Widget A is more balanced across regions.


Understanding groupby results

Tip

After a .groupby() operation, the resulting DataFrame has a specific structure:

  • The index becomes the grouping column(s) - the values we grouped by.
  • The columns are the aggregated values.

In our example, region_product_summary has a multi-level index (region and product) and two value columns (units_sold and revenue).

If we need the grouping columns back as regular columns rather than the index, we use .reset_index():

region_product_flat = region_product_summary.reset_index()
print(region_product_flat)
   region   product  units_sold  revenue
0  North  Gadget X         180     5400
1  North  Widget A         230     4600
2  North  Widget B         173     2595
3  South  Gadget X         395    11850
4  South  Widget A         135     2700
5  South  Widget B          92     1380

This converts the hierarchical index back to normal columns - useful for further analysis, plotting, or exporting.

Practice questions

3 questions

A DataFrame containing the Titanic dataset has been loaded into the Python Editor.

The Pclass column denotes the passenger's ticket class.

What was the survival rate of women in 1st class compared with men in 3rd class?

Select the correct answer:

+ 2 more questions