1
Hatchling0 / 300 XP
0day streak 0stars
Your Python practice
Mock tests Readiness
More
Guest mode · saved on this device.

Group totals: SUMIF and pivot tables

Spreadsheet SUMIF adds values whose category matches. groupby does this for every category at once:

totals = sales.groupby("region", as_index=False)["amount"].sum()

The result has one row per region. as_index=False keeps the group label in a normal column. sum() totals amounts; count() counts nonmissing values in the selected column; mean() averages them. Choose the operation that answers the business question.

Group by two categories with work.groupby(["team", "kind"], as_index=False)["minutes"].sum(). Each distinct team/kind pair gets its own total.

totals = totals.sort_values("region").reset_index(drop=True) sorts the region labels alphabetically and gives the result a fresh row index. For a team/kind summary, pass ["team", "kind"] to sort_values to sort by team first, then kind within each team. Store the returned table; these calls do not change the original.

A pivot table lays out two categories like a spreadsheet pivot:

grid = sales.pivot_table(index="region", columns="channel",
                         values="amount", aggfunc="sum", fill_value=0)

aggfunc="sum" matters: pivot_table defaults to an average. fill_value=0 means a category combination with no sales displays as zero. A pivot's row labels form its index: grid = grid.sort_index().sort_index(axis=1) sorts row labels, then column labels (axis=1). sort_values instead sorts rows using values in named columns.

See it run

import pandas as pd
spend = pd.DataFrame({"dept": ["ops", "hr", "ops", "hr", "it"], "cost": [10, 5, 20, 7, 3]})
by_dept = spend.groupby("dept")["cost"].sum()
flat = by_dept.reset_index()

groupby = split rows into piles by a key, then summarise each pile.

Practice group totals: sumif and pivot tables

Exercise 1

An office manager needs spending totals by department, like SUMIF in a budget sheet. Read team_spend.csv as spend. Create totals with columns department and amount, summed by department and sorted alphabetically by department with a fresh row index.

Exercise 2

A warehouse planner wants a shipping pivot. Load channel_units.csv as sales. Build grid with region as rows, channel as columns, and summed units as cells. Show zero for absent combinations. Sort both axes alphabetically.

Exercise 3

A service supervisor budgets time by team and work type. Read service_minutes.csv as work. Produce totals with team, kind, minutes: sum minutes for each team/kind pair, sort by team then kind alphabetically, and reset the row index. Do not combine a team's installation and repair budgets.

The explanation and tasks are public. Running this lesson requires an account with course access.

Practice

Loading Python…
More
Loading exercise content…
Visualizer Watch your code run line by line

Run your code, then choose Play to follow its execution.

Memory · names → values
Variables appear here as Python creates them.
Output & plots

One-line daily quest