Split / Explode a column of dictionaries into separate columns with pandas
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
If a pandas column contains dictionaries, the usual goal is to turn each dictionary key into a normal DataFrame column. The cleanest solution is to normalize that column and join the result back to the original frame.
The Basic Pattern
Suppose a DataFrame has a details column where each row contains a dictionary.
Output:
pd.json_normalize() is usually the best option because it is explicit and works well when dictionaries become more nested later.
apply(pd.Series) Also Works
For shallow dictionaries, you will often see this pattern:
This is readable and works well for simple cases. In practice, json_normalize is often the more flexible choice, especially if nested fields appear later.
Missing Keys Become Missing Values
Dictionary keys do not need to match perfectly across rows. Pandas will align by key name and fill missing values with NaN.
That behavior is usually what you want, but it means you may need to clean missing values afterward.
If The Column Contains JSON Strings, Parse First
Sometimes the column looks like dictionaries but actually contains strings.
If you skip parsing, pandas treats the values as plain strings and cannot split them into columns.
Distinguish A Dictionary Column From A List Column
The title often says "explode," but explode() is mainly for list-like values, not dictionaries.
Use:
- '
json_normalizeorapply(pd.Series)for a column of dictionaries' - '
explode()for a column of lists'
If you have a list of dictionaries, you may need both steps.
This distinction prevents a lot of confusion.
Keep Column Names Predictable
If the dictionaries are nested, json_normalize can create dotted column names such as address.city. That is often useful, but you may want to rename them afterward.
Clean column names early so later analysis stays readable.
Common Pitfalls
The most common mistake is calling explode() on a dictionary column. That is the wrong tool unless the values are lists.
Another mistake is forgetting to parse JSON strings before normalization.
Developers also sometimes expand the dictionaries correctly but forget to drop the original column, leaving duplicated information in the frame.
Finally, missing keys are normal. Treat the resulting NaN values as part of the transformation, not as a sign that pandas failed.
Summary
- Use
pd.json_normalize()to split a dictionary column into normal columns. - '
apply(pd.Series)is fine for simple flat dictionaries.' - Parse JSON strings with
json.loadsbefore expanding them. - Use
explode()only for list-like columns, not plain dictionaries. - Expect missing keys to produce
NaNand clean them afterward if needed.
Related reading
- Split a dataset created by Tensorflow dataset API in to Train and Test?
- Split a large pandas dataframe
- Split a Pandas column of lists into multiple columns
- Split data directory into training and test directory with sub directory structure preserved
- Split a binary search Tree
- Split a List into smaller lists of N size
- Split a python list into other sublists i.e smaller lists
- Split a string by a delimiter in Python

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 courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.