Skip to content
KALANBLON
Show contents

Chapter 5 of 6 · 25 min

Project: a cooperative's dashboard

By the end of this chapter, you'll be able to

  • Compute a dashboard's key indicators with pandas
  • Compose a page with key figures, a bar chart and a line chart
  • Check each chart's honesty with a checklist
  • Adapt the dashboard to a phone screen

The president's three questions

A dashboard answers a few precise questions, not all of them. Here:

  1. How much was sold this month, and is it more or less than last month? → two key figures.
  2. Where do we sell the most? → a sorted bar chart.
  3. How are sales changing since the start of the season? → a line chart.

Each chart passes the checklist before being shown.

Step 5 of 5
Text description of the animation
  1. We start from detailed sales, never from numbers copied by hand.
  2. pandas computes all the indicators at once, from the same data.
  3. Two big figures, readable in a second, with the direction of the change.
  4. The charts answer the “where” and “how” questions.
  5. Each chart passes the checklist before being shown.

Step 1: the indicators

import pandas as pd

sales = pd.DataFrame({
    "month":  ["sep"] * 3 + ["oct"] * 3 + ["nov"] * 3,
    "market": ["Bamako", "Koutiala", "Sikasso"] * 3,
    "kg":     [520, 410, 250, 610, 450, 300, 560, 440, 250],
})
order = ["sep", "oct", "nov"]
by_month = sales.groupby("month")["kg"].sum().reindex(order)
change = (by_month.iloc[-1] - by_month.iloc[-2]) / by_month.iloc[-2] * 100
by_market = sales[sales["month"] == "nov"].groupby("market")["kg"].sum().sort_values(ascending=False)

print("by month:", by_month.to_dict())
print(f"november: {by_month.iloc[-1]} kg ({change:+.1f}% compared with october)")
print("by market in november:", by_market.to_dict())
by month: {'sep': 1180, 'oct': 1360, 'nov': 1250}
november: 1250 kg (-8.1% compared with october)
by market in november: {'Bamako': 560, 'Koutiala': 440, 'Sikasso': 250}

The format {change:+.1f} always writes the sign (+ or -) and one decimal: a change without a sign is a classic source of misunderstanding. The figures are fictitious, as throughout the course.

Step 2: the page

The page assembles the key figures and both charts. The data is copied from pandas' output; in a real application, it would be loaded from a JSON file produced by the Python script.

Mini-lab: the dashboardHTML · CSS · JS

Preview

Console

(Nothing in the console yet.)

Your code runs in an isolated page, with no access to this site or the Internet · Ctrl/⌘ + Enter to run · Esc then Tab to leave the editor

Step 3: the checklist

Before showing a chart, check each point:

☐ The title says what, where, when and in which unit.
☐ Bars start at zero; a line chart's axis is labelled if it does not start at zero.
☐ Categories are in a useful order: sorted, or in calendar order.
☐ Important values are written, not only suggested by the shape.
☐ Changes have a sign and a reference period ("compared with October").
☐ The data source and whether it is real or fictitious are stated.
☐ The chart is readable on a phone, and colour is never the only information.

Your turn

Worked exercise

Project: your dashboard, checked

  1. Add a fourth month to the data, December, with 600 kg in Bamako, 480 kg in Koutiala and 300 kg in Sikasso, and recompute the indicators with pandas.
  2. Update the mini-lab's dashboard: key figures, December's bars and a four-point line.
  3. Go through the checklist and fix what is wrong.
Hint 1
For the four-point line, the horizontal gap must shrink: x = (i) => 40 + i * 85.
Hint 2
The vertical scale must cover the largest value: check that 1,500 kg is still enough.
Show the solution and common mistakes
import pandas as pd

sales = pd.DataFrame({
    "month":  ["sep"] * 3 + ["oct"] * 3 + ["nov"] * 3 + ["dec"] * 3,
    "market": ["Bamako", "Koutiala", "Sikasso"] * 4,
    "kg":     [520, 410, 250, 610, 450, 300, 560, 440, 250, 600, 480, 300],
})
by_month = sales.groupby("month")["kg"].sum().reindex(["sep", "oct", "nov", "dec"])
change = (by_month.iloc[-1] - by_month.iloc[-2]) / by_month.iloc[-2] * 100
print(by_month.to_dict())
print(f"december: {by_month.iloc[-1]} kg ({change:+.1f}% compared with november)")
{'sep': 1180, 'oct': 1360, 'nov': 1250, 'dec': 1380}
december: 1380 kg (+10.4% compared with november)

In the mini-lab, replace byMonth and byMarket with these new values, tighten the points (x = (i) => 40 + i * 85) and update the titles ("December", "compared with November") as well as the aria-label texts. The 1,500 kg scale still covers the maximum (1,380 kg).

Common mistakes:

  • Forgetting to update the alternative text (aria-label): a person using a screen reader still hears November's figures.
  • Leaving the old "November" title: the dashboard then shows correct figures under a wrong title.
  • Recomputing the change by hand instead of reusing pandas' output: one calculation error and the sign flips.

Summary

  1. 1A dashboard answers a few precise questions: how much, where, how it is changing.
  2. 2All the indicators are computed with pandas from the same detailed data.
  3. 3A change is written with its sign and its reference period.
  4. 4Each chart passes a checklist: title, zero, order, values, source, readability.
  5. 5Periods or groups of different sizes are compared after being brought back to a common base.

Your progress is saved on this device. Create a free account to keep your progress on all your devices.