ME204 2026 Icon

πŸ–₯️ Week 02, Day 04 - Lecture

Untangling Nested JSON: the pandas way

Author

Dr Jon Cardoso-Silva

Last updated

23 July 2026

πŸ₯… Learning Objectives

By the end of the lecture, you should be able to: i) Write a Python function with a docstring, call it inside a loop, and convert that loop into a list comprehension, ii) Explain the difference between a list of dictionaries and a dictionary of lists, and convert between them with a function, iii) Use pd.json_normalize() with record_path and meta to flatten nested API responses into a DataFrame, iv) Recognise when json_normalize cannot unpack a nested list and use pd.concat() to stitch the pieces together.

⏰ Thursday, 23 July 2026 | 10:00am - 1:00pm πŸ“ CBG.2.01 (see LSE’s πŸ—ΊοΈ campus map)

Yesterday you put your work under Git. Today you learn how to write functions, reshape JSON with them, and let pd.json_normalize do the heavy lifting.

πŸ—£οΈ Lecture Overview

  • Python functions: def, parameters, return, docstrings, list comprehensions, lambda
  • Reshaping JSON with functions: pd.DataFrame on both shapes, writing a records_to_columns function, generalising it, handling nested responses
  • Coffee break
  • pd.json_normalize: record_path, meta, nested dicts (automatic flattening), nested lists (not flattened), breaking the problem into pieces with pd.concat
  • Code conventions going forward: when to use loops, when to use pandas

🎬 Lecture Slides

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

After the Lecture

This afternoon you use functions and pd.json_normalize on a new API (PokΓ©API), flatten nested stats with pd.concat, and move your functions into an importable utils.py.

πŸ’» Afternoon lab

Nested JSON with PokΓ©API.

➑️ Lab page

Questions?

Ask your class teacher during the lab.