Sub topic 8
Turning a list of records into rows with record_path
This chapter assumes you can call pd.json_normalize on a list of dicts and that you know how dotted column names are built. Those topics came earlier. Here the goal is different. Some of your data is a list of records, such as the items in an order, and you want one row per list element.
Why plain flattening is not enough
When a value is a list, json_normalize leaves it in a single cell. An order with three items still produces one row, and the items sit inside it as a list of dicts. That is hard to filter, group, or sum.
The record_path argument tells json_normalize which key holds the list to expand. Each element of that list becomes one row.
Expanding one list
The data below is a list of orders. Each order has an items list.
import pandas as pd
orders = [
{
"order_id": 100,
"customer": {"id": 1, "name": "Ada"},
"items": [{"sku": "A1", "qty": 2}, {"sku": "B2", "qty": 1}],
},
{
"order_id": 101,
"customer": {"id": 2, "name": "Grace"},
"items": [{"sku": "A1", "qty": 5}],
},
]
df = pd.json_normalize(orders, record_path="items")
print(df)
# sku qty
# 0 A1 2
# 1 B2 1
# 2 A1 5Three things to notice:
- The row count equals the total number of list elements across all orders, not the number of orders.
- Only the keys inside each list element become columns. Fields such as
order_idandcustomerare dropped. - The flattening rules from earlier still apply inside each element. If an item held a nested object like
{"price": {"amount": 3}}, you would get a column namedprice.amount.
Reaching a list through nested objects
Sometimes the list is not at the top level of the record. Pass a list of keys, and json_normalize walks them in order.
payload = {"data": {"items": [{"sku": "A1"}, {"sku": "B2"}]}}
df = pd.json_normalize(payload, record_path=["data", "items"])
print(df.columns.tolist())
# ['sku']
print(len(df))
# 2A single string such as "items" is shorthand for a path with one key. Passing a bare dict, as above, works the same as passing a one-element list of dicts.
Behavior to expect
| Situation | Result |
|---|---|
| A record's list is empty (`"items": []`) | That record adds zero rows and does not appear in the output |
| A record's list has n elements | That record adds n rows |
| A list element has keys the others lack | Those columns exist, with NaN where a row lacks the key |
The empty-list case is a common surprise. If you count orders after expanding, orders with no items are gone.
Common errors
- Pointing at the wrong key. If
record_pathnames a key that holds an object rather than a list, you will not get the rows you expect. Print one record first and check the shape. - A key missing from some records. By default, a record that lacks the
record_pathkey raises aKeyError. Usingerrors="ignore"skips such records instead, and a later chapter covers it. - Expecting parent columns. If
order_idis missing from the result, that is normal. It is not a bug.
Exercise
A list named shipments holds dicts like {"id": 7, "parcels": [{"weight": 1.2}, {"weight": 0.8}]}. There are 4 shipments, and the parcel lists have 2, 0, 3 and 1 elements. What call turns the parcels into rows, and how many rows does it return?
Show answer
pd.jsonnormalize(shipments, recordpath="parcels"). It returns 6 rows (2 + 0 + 3 + 1). The shipment with an empty list adds no rows, and the output has a single column named weight.