Convert Python Dictionary to Pandas DataFrame

When I was working on a data analysis project for a US retail company, I needed to transform their sales data from a Python dictionary into a more analyzable format. The issue was, working with nested dictionaries made it difficult to perform the calculations I needed.

That’s when I turned to Pandas DataFrames, which made the entire process much simpler.

In this article, I’ll share five practical methods to convert Python dictionaries to Pandas DataFrames. Whether you’re working with simple or complex nested dictionaries, these techniques will help you transform your data efficiently.

So let us start..

Pandas DataFrame

A Pandas DataFrame is a two-dimensional, size-mutable, and potentially heterogeneous tabular data structure with labeled axes (rows and columns). Think of it as a spreadsheet or SQL table that makes data manipulation much easier.

Before we start converting dictionaries, let’s make sure we have the necessary libraries installed:

import pandas as pd

Read Add Rows to a DataFrame Pandas in a Loop in Python

Convert Python Dictionary to Pandas DataFrame

Now, I will explain how to convert a Python dictionary to a Pandas DataFrame.

Method 1: Basic Dictionary to DataFrame Conversion

The simplest way to convert a dictionary to a DataFrame is by using the pd.DataFrame() constructor directly in Python.

Let’s say we have sales data for different states:

# Dictionary with sales data by state
sales_data = {
    'State': ['California', 'Texas', 'Florida', 'New York', 'Illinois'],
    'Sales': [45000, 29000, 31000, 42000, 27000],
    'Customers': [1200, 800, 950, 1100, 750]
}

# Convert to DataFrame
df = pd.DataFrame(sales_data)

print(df)

Output:

        State  Sales  Customers
0  California  45000       1200
1       Texas  29000        800
2     Florida  31000        950
3    New York  42000       1100
4    Illinois  27000        750

You can refer to the screenshot below to see the output.

dictionary to dataframe

In this example, each key in the dictionary becomes a column in the DataFrame, and the values become the data in those columns.

Check out Convert a Pandas DataFrame to a List in Python

Method 2: Convert a Dictionary of Series to a DataFrame

Sometimes, your data might be structured as a Python dictionary of Pandas Series. Here’s how to handle that:

# Dictionary of Series
california_data = pd.Series([45000, 1200, 350], index=['Sales', 'Customers', 'Products'])
texas_data = pd.Series([29000, 800, 250], index=['Sales', 'Customers', 'Products'])
florida_data = pd.Series([31000, 950, 280], index=['Sales', 'Customers', 'Products'])

state_data = {
    'California': california_data,
    'Texas': texas_data,
    'Florida': florida_data
}

# Convert to DataFrame
df = pd.DataFrame(state_data)

print(df)

Output:

           California  Texas  Florida
Sales          45000  29000    31000
Customers       1200    800      950
Products         350    250      280

You can refer to the screenshot below to see the output.

pandas from dict

In this case, each key in the dictionary becomes a column, and the Series indexes become the row labels.

Read Convert a Pandas DataFrame to a Dict Without Index in Python

Method 3: Convert a List of Dictionaries to a DataFrame

When handling data from APIs or JSON files, you’ll often encounter lists of dictionaries in Python. Here’s how to convert them:

# List of dictionaries (common in JSON data)
customer_records = [
    {'name': 'John Smith', 'state': 'California', 'purchase': 120},
    {'name': 'Emily Chen', 'state': 'New York', 'purchase': 95},
    {'name': 'Michael Johnson', 'state': 'Texas', 'purchase': 135},
    {'name': 'Sarah Williams', 'state': 'Florida', 'purchase': 85}
]

# Convert to DataFrame
df = pd.DataFrame(customer_records)

print(df)

Output:

              name       state  purchase
0      John Smith  California       120
1      Emily Chen    New York        95
2  Michael Johnson       Texas       135
3  Sarah Williams     Florida        85

You can refer to the screenshot below to see the output.

dict to dataframe

This method is particularly useful when working with data from external sources like web APIs.

Check out Convert a DataFrame to a Nested Dictionary in Python

Method 4: Convert Nested Dictionaries Using from_dict()

When dealing with nested dictionaries, the Python from_dict() method gives you more control:

# Nested dictionary
quarterly_sales = {
    'Q1': {'California': 12000, 'Texas': 8000, 'Florida': 9000},
    'Q2': {'California': 11000, 'Texas': 7500, 'Florida': 8500},
    'Q3': {'California': 10500, 'Texas': 7000, 'Florida': 7500},
    'Q4': {'California': 11500, 'Texas': 8500, 'Florida': 8000}
}

# Convert to DataFrame with orient='index'
df1 = pd.DataFrame.from_dict(quarterly_sales, orient='index')
print("DataFrame with quarters as index:")
print(df1)
print("\n")

# Convert with different orientation
df2 = pd.DataFrame.from_dict(quarterly_sales)
print("DataFrame with states as columns:")
print(df2)

Output:

DataFrame with quarters as index:
    California  Texas  Florida
Q1       12000   8000     9000
Q2       11000   7500     8500
Q3       10500   7000     7500
Q4       11500   8500     8000

DataFrame with states as columns:
    California  Texas  Florida
Q1       12000   8000     9000
Q2       11000   7500     8500
Q3       10500   7000     7500
Q4       11500   8500     8000

The orient parameter lets you control how the nested dictionary is interpreted. With orient='index' the outer keys become row indexes, while the inner keys become column names.

Read Drop Rows in Python Pandas DataFrames

Method 5: Convert Dictionary with Irregular Data

Sometimes, your dictionary might contain irregular data with different lengths. Let’s see how to handle that:

# Dictionary with irregular data
irregular_data = {
    'Product': ['Laptop', 'Phone', 'Tablet', 'Monitor', 'Keyboard'],
    'Price': [1200, 800, 350, 250, 80],
    'Stock': [45, 120, 85, 30],  # Missing one value
    'Rating': [4.8, 4.6, 4.2]     # Missing two values
}

# Convert to DataFrame (with ignore_index=True to reset the index)
df = pd.DataFrame(irregular_data)

print(df)

Output:

    Product  Price  Stock  Rating
0    Laptop   1200   45.0     4.8
1     Phone    800  120.0     4.6
2    Tablet    350   85.0     4.2
3   Monitor    250   30.0     NaN
4  Keyboard     80    NaN     NaN

Pandas automatically fills missing values with NaN (Not a Number) when columns have different lengths.

Check out Use Pandas to Convert Float to Int in Python

Use Orient Parameter for More Control Over Conversion

The orient parameter in the DataFrame constructor gives you flexibility in how your dictionary is interpreted:

# Sample dictionary
product_data = {
    'Laptop': {'Price': 1200, 'Stock': 45, 'Rating': 4.8},
    'Phone': {'Price': 800, 'Stock': 120, 'Rating': 4.6},
    'Tablet': {'Price': 350, 'Stock': 85, 'Rating': 4.2}
}

# Different orient options
print("orient='dict' (default):")
df1 = pd.DataFrame.from_dict(product_data)
print(df1)
print("\n")

print("orient='index':")
df2 = pd.DataFrame.from_dict(product_data, orient='index')
print(df2)

Output:

orient='dict' (default):
       Laptop  Phone  Tablet
Price    1200    800     350
Stock      45    120      85
Rating    4.8    4.6     4.2

orient='index':
        Price  Stock  Rating
Laptop   1200     45     4.8
Phone     800    120     4.6
Tablet    350     85     4.2

The orient parameter can take different values:

  • 'dict' (default): Dictionary keys become column labels
  • 'index': Dictionary keys become row labels
  • 'columns': Dictionary keys become column labels
  • 'list': Dictionary values become rows

Convert Dictionary to Pandas DataFrame and Set Dictionary Index

Sometimes you might want to use one of the dictionary keys as the index while converting dictionary to Pandas DataFrame:

# Dictionary with potential index column
employee_data = {
    'ID': ['E001', 'E002', 'E003', 'E004'],
    'Name': ['John Smith', 'Emily Chen', 'Michael Johnson', 'Sarah Williams'],
    'Department': ['Sales', 'Marketing', 'IT', 'HR'],
    'Salary': [75000, 82000, 95000, 68000]
}

# Convert to DataFrame and set 'ID' as index
df = pd.DataFrame(employee_data).set_index('ID')

print(df)

Output:

                Name Department  Salary
ID                                     
E001      John Smith      Sales   75000
E002      Emily Chen  Marketing   82000
E003  Michael Johnson         IT   95000
E004  Sarah Williams         HR   68000

This makes it easier to access records by their ID, which is often more intuitive.

Read Print the First 10 Rows from a Pandas DataFrame in Python

Handle Multi-level Dictionaries During Conversion of Dictionary to Pandas DataFrame

For complex nested dictionaries with multiple levels, you might need to flatten them first:

import pandas as pd
import json

# Multi-level nested dictionary
complex_data = {
    'Northeast': {
        'New York': {'Q1': 15000, 'Q2': 14500},
        'Massachusetts': {'Q1': 9000, 'Q2': 8500}
    },
    'West': {
        'California': {'Q1': 20000, 'Q2': 21000},
        'Oregon': {'Q1': 7500, 'Q2': 8000}
    }
}

# Flatten dictionary and convert to DataFrame
flattened_data = []
for region, states in complex_data.items():
    for state, quarters in states.items():
        for quarter, sales in quarters.items():
            flattened_data.append({
                'Region': region,
                'State': state,
                'Quarter': quarter,
                'Sales': sales
            })

df = pd.DataFrame(flattened_data)
print(df)

Output:

     Region          State Quarter  Sales
0  Northeast       New York      Q1  15000
1  Northeast       New York      Q2  14500
2  Northeast  Massachusetts      Q1   9000
3  Northeast  Massachusetts      Q2   8500
4       West     California      Q1  20000
5       West     California      Q2  21000
6       West         Oregon      Q1   7500
7       West         Oregon      Q2   8000

This approach works well for hierarchical data that needs to be analyzed in a flattened format.

I hope you found this article helpful for converting Python dictionaries to Pandas DataFrames. As you can see, Pandas provides multiple flexible ways to transform dictionary data into a structured format that’s perfect for data analysis.

The methods that I explained in this tutorial are the basic dictionary to DataFrame conversion, converting a dictionary of series to a DataFrame, a list of dictionaries to a DataFrame, nested dictionaries using from_dict(), and converting a dictionary with irregular data.

You may like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.