FormulaCamp
Completed
15. Python in Excel and AI · pandas and Python Charts · 20 min

3. pandas: groupby and agg

Lesson 3 of 6
Task 1 of 5
  1. In F2:F4, repeat df.groupby("City")["Amount"].sum().

Lesson theory

Grouping is the most common operation in pandas. Rows are split into groups by the value of a column, and a total is calculated for each group:

df = xl("A1:C11", headers=True)
df.groupby("City")["Amount"].sum()                    # sum by city
df.groupby("City")["Amount"].agg(["sum", "count", "mean"])
df.groupby(["City", "Manager"])["Amount"].sum()       # grouping by two keys

The result of agg is a small table: rows are cities, columns are sum, count and mean. In regular Excel you get the same with SUMIF, COUNTIF and AVERAGEIF, and grouping by two keys with SUMIFS. Checking "Python against formulas" catches mistakes in the code, such as a forgotten column or the wrong range in xl().

An important check: the sum of the group totals must equal the overall total. If it doesn't, some rows didn't land in any group (for example, a city was misspelled).

Test yourself

1. What does df.groupby("City")["Amount"].count() return?
2. The sum of the groupby totals is less than the overall total. What is the most likely cause?
3. Which Excel formula checks df.groupby(["City","Manager"])["Amount"].sum()?

Practice is part of Pro

The live spreadsheet, task checking and .xlsx files in all 170 lessons. Your progress is saved in this browser; sync across devices is coming soon.