Python · Advanced level

Advanced level · Module 5: Data Analysis with pandas · Lesson 4 of 4

Summarising and Charting Results

Aggregate data with groupby and pivot tables and present findings with clear charts.

In this lesson you will

  • Summarise with groupby and pivot_table
  • Create labelled bar and line charts

Key concepts

groupby/aggpivot_tablematplotlibchart labelling

The lesson

groupby() summarises data by category: df.groupby('category')['amount'].sum() gives total sales per category. pivot_table() builds two-way summaries like a spreadsheet PivotTable.

Charts make patterns visible. pandas plots with Matplotlib: choose a bar chart to compare categories and a line chart to show change over time. Always label axes and include units such as SSP.

A good summary states what the numbers show and what should be done about it. Charts support the message; they do not replace it.

Step by step

  1. Group sales by month and category.
  2. Create a bar chart and a line chart with labelled axes.
  3. Export the charts as PNG and the summary table to Excel.
  4. Write three sentences interpreting the results.
Data and chart

Reading a price trend with pandas

Summarise monthly prices with groupby and turn the result into a chart and a recommendation.

Average price of a 3.5 kg malwa of maize, illustrative training data (SSP)
MonthMarket AMarket B
Jan4,2004,500
Feb4,4004,600
Mar4,9005,100
Apr5,6005,800
May6,1006,500
Jun5,8006,200
Monthly average maize price by market Market A: Jan 4,200, Feb 4,400, Mar 4,900, Apr 5,600, May 6,100, Jun 5,800. Market B: Jan 4,500, Feb 4,600, Mar 5,100, Apr 5,800, May 6,500, Jun 6,200 01,7503,5005,2507,000JanFebMarAprMayJunMarket AMarket B

What the data shows

The table is the output of df.groupby(['month', 'market'])['price'].mean().unstack(). Both markets rise steadily from January to May — about 45% — then ease slightly in June.

Market B is consistently 200–400 SSP more expensive, so the gap is structural rather than a one-off.

Recommendation

A school feeding programme buying monthly should agree prices or buy stock before March, and should source from Market A where transport allows. Treat the figures as illustrative: use real, dated market data for decisions.

Practice activity

Produce a one-page summary of monthly sales by category with a table and two charts, exported as PNG and Excel.

Expected result: A summary table and two correctly labelled charts.

Check your understanding

  1. What three labels should every chart have?

    Show answer

    A title and labelled axes including units.

Taught live by an instructor — online or in person. Free course lessons are for self-study and do not lead to a certificate — see registration options.