Construct pandas DataFrame from items in nested dictionary

Given a nested dictionary, we have to construct Pandas DataFrame from it items.
Submitted by Pranit Sharma, on July 02, 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.

To create pandas DataFrame from items in a nested dictionary, 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.

To work with pandas, we need to import pandas package first, below is the syntax:

import pandas as pd

Let us understand with the help of an example,

Python code to construct pandas DataFrame from items in nested dictionary

# Importing pandas package
import pandas as pd

# Creating a nested dictionary
d = {

# 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: Construct pandas DataFrame

Python Pandas Programs »


Top MCQs

Comments and Discussions!

© some rights reserved.