Python - Create pandas dataframe from dictionary of dictionaries

Given dictionary of dictionaries, we have to create pandas dataframe from it.
Submitted by Pranit Sharma, on August 07, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Dictionaries are used to store heterogeneous data. The data is stored in key:value pair. A dictionary is a mutable collection and ordered in nature and does not allow duplicates which mean there are unique keys in a dictionary.

A dictionary key can have any type of data as its value, for example, a list, tuple, string, or a dictionary itself.

If a key's value is a dictionary, it is called a nested dictionary. Suppose multiple keys have dictionaries as their values and we have to create a pandas DataFrame from this nested dictionary, pandas will help us to achieve this task.

To create pandas DataFrame from the dictionary of dictionaries, we will use the following syntax:


If we have a very huge nested dictionary, this method allows us to extract keys inside keys and fetch their values to create columns and rows of the DataFrame.

Let us understand with the help of an example,

Python code to create pandas dataframe from dictionary of dictionaries

# Importing pandas package
import pandas as pd

# Creating a nested dictionary
d = {'Gwalior':

# Creating a DataFrame
df = pd.DataFrame.from_dict({(i,j): d[i][j] for i in d.keys() for j in d[i].keys()},orient='index')

# Display DataFrame
print("Created DataFrame:\n",df)


Example: Create pandas dataframe from dictionary of dictionaries

Python Pandas Programs »

Comments and Discussions!

Copyright © 2023 All rights reserved.