DS105 2025-2026 Winter Term Icon

đŸ–Ĩī¸ Week 08 Lecture

Pandas Workshop: reshaping, pivoting, and merging data

Author

Dr Jon Cardoso-Silva

Published

19 March 2026

đŸĨ… Learning Goals

By the end of this lecture, you should be able to: i) stack DataFrames with pd.concat(), ii) reshape wide → long with .melt() for seaborn, iii) reshape long → wide with .pivot_table() including custom aggfunc, iv) connect two DataFrames on a shared key with pd.merge() and check merge quality.

📍 Logistics

📍Location: Thursday, 12 March 2026, 4-6 pm at CKK.LG.03

Today is a practical pandas workshop. We will walk through four transformation tools with colour-coded visual examples and live code. If time allows, we will do a group challenge in Hour 2.

📋 Preparation

đŸ—Ŗī¸ Lecture Overview

  • pd.concat(): stack DataFrames from separate route files (documentation)

  • .melt(): reshape wide summary tables to long format (documentation)

    • That will allow you to use the hue parameter on seaborn plots more easily (documentation)
  • .pivot() and .pivot_table(): to reshape long → wide data (documentation)

    • The aggfunc parameter allows you to specify how to aggregate values when pivoting (e.g. mean, sum, count, etc.)
  • pd.merge(): join your journey data with the ONS Postcode Directory on a postcode key (documentation)

📓 Lecture Materials

Today we use facilitation slides plus one lecture notebook. The notebook will be shared on Nuvolos, and you can also download a zip bundle with all Week 08 files used in the notebook.

đŸŽŦ Facilitation Slides

Use keyboard arrows to navigate. Select the slides below or view fullscreen.

Or download the slides directly as a PDF:

đŸ“Ĩ Demo Notebook

The demo notebook walks through every transformation shown in the lecture slides, using synthetic TfL-like journey data.

🔖 Appendix

Useful Links

Looking Ahead

  • Friday W08 Lab: merge your own TfL data with the ONS Postcode Directory and build NB02
  • Week 09: EDA quality checks and visual communication critique
  • Week 10: MP2 submission deadline (Monday 23 March, 8 pm)