π₯οΈ Week 02, Day 04 - Lecture
Untangling Nested JSON: the pandas way
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.DataFrameon both shapes, writing arecords_to_columnsfunction, 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 withpd.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.
Questions?