Sling Academy
Home/Pandas/Pandas: Convert a list of dicts into a DataFrame

Pandas: Convert a list of dicts into a DataFrame

Last updated: February 19, 2024

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.

Next Article: Pandas: Turn a DataFrame to a list of dictionaries

Previous Article: Pandas data types cheat sheet

Series: DateFrames in Pandas

Pandas

You May Also Like

  • How to Use Pandas Profiling for Data Analysis (4 examples)
  • How to Handle Large Datasets with Pandas and Dask (4 examples)
  • Pandas – Using DataFrame.pivot() method (3 examples)
  • Pandas: How to ‘FULL JOIN’ 2 DataFrames (3 examples)
  • Pandas: Select columns whose names start/end with a specific string (4 examples)
  • 3 ways to turn off future warnings in Pandas
  • How to Integrate Pandas with Apache Spark
  • How to Use Pandas for Web Scraping and Saving Data (2 examples)
  • How to Clean and Preprocess Text Data with Pandas (3 examples)
  • Pandas – Using Series.replace() method (3 examples)
  • Pandas json_normalize() function: Explained with examples
  • Pandas: Reading CSV and Excel files from AWS S3 (4 examples)
  • Using pandas.Series.rank() method (4 examples)
  • Pandas: Dropping columns whose names contain a specific string (4 examples)
  • Pandas: How to print a DataFrame without index (3 ways)
  • Fixing Pandas NameError: name ‘df’ is not defined
  • Pandas – Using DataFrame idxmax() and idxmin() methods (4 examples)
  • Pandas FutureWarning: ‘M’ is deprecated and will be removed in a future version, please use ‘ME’ instead
  • Pandas: Checking equality of 2 DataFrames (element-wise)