pandas
data manipulation
Python
dataframe transformation
dictionary columns

Split or explode a column of dictionaries into separate columns with pandas

Codemia Knowledge Hub ·

To turn a pandas column of dictionaries into one column per key, normalize the column with pd.json_normalize() and join the result back to the original frame, dropping the original column.

Basic pattern with json_normalize

Suppose each row of a details column holds a dictionary:

python
1import pandas as pd
2
3
4df = pd.DataFrame({
5    "id": [1, 2, 3],
6    "details": [
7        {"name": "Alice", "age": 25},
8        {"name": "Bob", "age": 30},
9        {"name": "Cara", "age": 28},
10    ],
11})
12
13expanded = pd.json_normalize(df["details"])
14result = pd.concat([df.drop(columns=["details"]), expanded], axis=1)
15print(result)

Output:

text
1   id   name  age
20   1  Alice   25
31   2    Bob   30
42   3   Cara   28

pd.json_normalize() is usually the best option because it is explicit and copes well if the dictionaries become more nested later. Remember to drop the original column, or the frame keeps duplicated information.

apply(pd.Series) for flat dictionaries

For shallow dictionaries you will often see:

python
expanded = df["details"].apply(pd.Series)
result = df.drop(columns=["details"]).join(expanded)

It is readable and fine for simple cases, but json_normalize is more flexible once nested fields appear.

Missing keys become NaN

Keys do not have to match across rows. Pandas aligns by key name and fills gaps with NaN:

python
1import pandas as pd
2
3
4df = pd.DataFrame({
5    "details": [
6        {"name": "Alice", "age": 25},
7        {"name": "Bob"},
8        {"name": "Cara", "city": "Toronto"},
9    ]
10})
11
12expanded = pd.json_normalize(df["details"])
13print(expanded)

That is normal and usually what you want, not a sign that pandas failed. Clean the gaps afterward if needed:

python
expanded = expanded.fillna({"age": 0, "city": "unknown"})

Parse JSON strings first

Sometimes the column looks like dictionaries but actually holds strings:

python
1import json
2import pandas as pd
3
4
5df = pd.DataFrame({
6    "details": [
7        '{"name": "Alice", "age": 25}',
8        '{"name": "Bob", "age": 30}'
9    ]
10})
11
12df["details"] = df["details"].apply(json.loads)
13expanded = pd.json_normalize(df["details"])
14print(expanded)

Without parsing, pandas treats the values as plain strings and cannot split them into columns.

Dictionary columns versus list columns

Despite the word "explode," explode() is for list-like values, not dictionaries. Calling it on a dictionary column is the most common mistake. Use:

  • json_normalize or apply(pd.Series) for a column of dictionaries
  • explode() for a column of lists

A column of lists of dictionaries needs both steps:

python
1import pandas as pd
2
3
4df = pd.DataFrame({
5    "id": [1],
6    "items": [[{"name": "A", "qty": 2}, {"name": "B", "qty": 5}]],
7})
8
9exploded = df.explode("items", ignore_index=True)
10expanded = pd.json_normalize(exploded["items"])
11result = pd.concat([exploded.drop(columns=["items"]), expanded], axis=1)
12print(result)

Renaming dotted columns from nested dictionaries

With nested dictionaries, json_normalize produces dotted names such as address.city. That is often useful, but you can rename them:

python
expanded = pd.json_normalize(df["details"])
expanded = expanded.rename(columns={"address.city": "city"})

Clean column names early so later analysis stays readable.


Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design