Pandas: Convert a list of dicts into a DataFrame

Updated: February 19, 2024 By: Guest Contributor Post a comment

Introduction

Pandas is a powerful and versatile toolkit for data analysis and manipulation in Python, particularly useful for working with structured data. One common task you might encounter is converting a list of dictionaries into a pandas DataFrame. This article will guide you through this process, from basic to more advanced examples, including real-world scenarios and outputs.

Getting Started

Before diving into the various ways to convert a list of dicts into a DataFrame, ensure Pandas is installed and imported in your Python environment:

import pandas as pd

If you haven’t installed Pandas yet, you can do so using pip:

pip install pandas

Basic Example

Consider a list of dictionaries where each dictionary contains data about a person:

data = [
  {'Name': 'John Doe', 'Age': 30, 'City': 'New York'},
  {'Name': 'Jane Smith', 'Age': 25, 'City': 'Chicago'},
  {'Name': 'Dave Brown', 'Age': 45, 'City': 'Los Angeles'}
]

To convert this into a DataFrame:

df = pd.DataFrame(data)

Output:

        Name  Age        City
0   John Doe   30  New York
1 Jane Smith   25    Chicago
2 Dave Brown   45 Los Angeles

Handling Uneven Data

Not all dictionaries in your list may have the same keys. Pandas handles this gracefully by filling missing keys with NaN values. Here’s an example:

data = [
  {'Name': 'John Doe', 'Age': 30, 'City': 'New York', 'Occupation': 'Developer'},
  {'Name': 'Jane Smith', 'Age': 25},
  {'Name': 'Dave Brown', 'Age': 45, 'City': 'Los Angeles'}
]

To convert:

df = pd.DataFrame(data)

Output:

        Name  Age        City  Occupation
0   John Doe   30  New York    Developer
1 Jane Smith   25  NaN         NaN
2 Dave Brown   45  Los Angeles NaN

Specifying Column Order

You may want to specify the order of columns when creating your DataFrame. You can do this by passing a list of column names to the DataFrame constructor:

df = pd.DataFrame(data, columns=['Name', 'Age', 'City', 'Occupation'])

This ensures your DataFrame always has a consistent column order, even if some dictionaries might be missing certain keys.

Advanced Example: Nested Dictionaries

In some cases, your list might contain nested dictionaries, representing more complex data structures. To flatten these into a DataFrame, use json_normalize:

from pandas.io.json import json_normalize

Example:

data = [
  {'Name': 'John Doe', 'Age': 30, 'Info': {'Height': '6ft', 'Weight': '180lbs'}},
  {'Name': 'Jane Smith', 'Age': 25, 'Info': {'Height': '5ft5in', 'Weight': '125lbs'}},
  {'Name': 'Dave Brown', 'Age': 45, 'Info': {'Height': '5ft10in', 'Weight': '175lbs'}}
]

Then use json_normalize to convert:

df = json_normalize(data)

Output:

        Name  Age Info.Height Info.Weight
0   John Doe   30         6ft      180lbs
1 Jane Smith   25      5ft5in      125lbs
2 Dave Brown   45      5ft10in      175lbs

Dealing with Large Datasets

When dealing with larger datasets, converting a list of dictionaries to a DataFrame may consume significant memory and processing time. To optimize performance, consider processing your data in chunks or utilizing data types that consume less memory.

Some datasets for practice:

Conclusion

Converting a list of dictionaries to a DataFrame in Pandas is straightforward, yet powerful. It allows for the quick assembly of variously structured data into a format suitable for further analysis, manipulation, and visualization. Whether your data is simple or complex, Pandas provides the tools needed to transform it efficiently into a valuable resource for your data projects.