How to Convert Dictionary to Pandas DataFrame

You may use the following template to convert a dictionary to pandas DataFrame:

from pandas import DataFrame

my_dict = {key:value,key:value,key:value,...}
df = DataFrame(list(my_dict.items()),columns = ['column1','column2']) 

In this short tutorial, I’ll review the steps to convert a dictionary to pandas DataFrame. I’ll also share the code to create the following tool to convert your dictionary to a DataFrame:

How to Convert Dictionary to Pandas DataFrame

Steps to Convert Dictionary to Pandas DataFrame

Step 1: Gather the Data for the Dictionary

To start, gather the data for your dictionary.

For example, I gathered the following data about products and prices:

ProductPrice
Computer1500
Monitor300
Printer150
Desk250

Step 2: Create the Dictionary

Next, create the dictionary.

For our example, you may use the following code to create the dictionary:

my_dict = {'Computer':1500,'Monitor':300,'Printer':150,'Desk':250}

print (my_dict)

Run the code in Python, and you’ll get this dictionary:

Dictionary in Python

Step 3: Convert the Dictionary to a DataFrame

Finally, convert the dictionary to a DataFrame using this template:

from pandas import DataFrame

my_dict = {key:value,key:value,key:value,...}
df = DataFrame(list(my_dict.items()),columns = ['column1','column2']) 

For our example, this is the complete Python code to convert the dictionary to the DataFrame:

from pandas import DataFrame

my_dict = {'Computer':1500,'Monitor':300,'Printer':150,'Desk':250}
df = DataFrame(list(my_dict.items()),columns = ['Products','Prices'])

print (df)

Run the code, and you’ll get the DataFrame below:

DataFrame in Python

Tool to Convert Dictionaries

In the last section of this tutorial, I’ll share with you the code to create a tool to convert dictionaries to DataFrames.

The code is based on the tkinter package that can be used to create a Graphical User Interface (GUI) in Python.

Here is the complete code:

from pandas import DataFrame
import tkinter as tk

root= tk.Tk()

canvas1 = tk.Canvas(root, width = 500, height = 500,  relief = 'raised')
canvas1.pack()

label1 = tk.Label(root, text='Convert Dictionary to DataFrame')
label1.config(font=('helvetica', 14))
canvas1.create_window(250, 25, window=label1)

label2 = tk.Label(root, text='Input Your Dictionary:')
label2.config(font=('helvetica', 10))
canvas1.create_window(250, 100, window=label2)

entry1 = tk.Entry (root, width = 70) 
canvas1.create_window(250, 140, window=entry1)

def getDataFrame ():
    
    my_dict = str(entry1.get())
    my_dict = eval(my_dict)
    df = DataFrame(list(my_dict.items()),columns= ['Products','Prices'])
    print (df)
    
    label3 = tk.Label(root, text=df,font=('helvetica', 10, 'bold'))
    canvas1.create_window(250, 270, window=label3)
    
button1 = tk.Button(text='Get DataFrame', command=getDataFrame, bg='brown', fg='white', font=('helvetica', 9, 'bold'))
canvas1.create_window(250, 180, window=button1)

root.mainloop()

Once you run the code, you’ll see the following GUI:

tkinter GUI

Copy the following dictionary into the entry box:

{'Computer':1500,'Monitor':300,'Printer':150,'Desk':250}

How to Convert Dictionary to Pandas DataFrame

Finally, click on the red button to get the DataFrame:

Dictionary to Pandas DataFrame

You may input a different dictionary into the tool in order to get your DataFrame.