How to Add Dictionary Keys and Values as Pandas Columns?

As a Data Scientist programmer, you have to work most on the Python Dictionary and lists. You use it with Pandas for creating a beautiful and exporting table for your data present as a list and the dictionary. But converting dictionary keys and values as Pandas columns is always leads to time-consuming if you don’t know the concept of using it. In this entire tutorial of “how to “, you will learn how to convert python dictionary to a pandas data frame in simple steps.

Step 1: Import the necessary libraries

Here I will use only the pandas library for creating dataframe.

import pandas as pd

Step 2: Create a List of Dictionary items

Before converting a dictionary into the data frame let’s create a sample dictionary. Let’s say there are two keys to it that are the name of the country and its capital.  I will make a list of all the dictionary that represents the keys and value field in each dictionary.

country_list = [] 
country_list.append({"Country":"India","Capital":"New Delhi"})
country_list.append({"Country":"USA","Capital":"Washington, D.C."})
country_list.append({"Country":"Australia","Capital":"Canberra"})
country_list.append({"Country":"UK","Capital":"London"})

Step 3: Create a Dataframe

Now when you get the list of dictionaries then You will use the pandas function DataFrame() to modify it into dataframe. Use the following code.

df = pd.DataFrame(country_list)
df

It will create the Dataframe table with Country and Capital keys as Columns and their values as a row. In our example, there are Four countries and Four capital.

dataframe for country and its capital

Other Cases

There are also some other cases when you are unable to get proper results. For example, I have a dictionary of dictionary inside the list. Then how you can convert it into DataFrame? Let’s Understand it.

Suppose I have list of stock signals in a format like this.

signal =[{'pattern': {'signal': 'Bear', 'date': '2020-01-11'}, 'code': 532648, 'company_name': 'YESBANK'},
{'pattern': {'signal': 'Bull', 'date': '2020-01-11'}, 'code': 532839, 'company_name': 'DISHTV'}, 
{'pattern': {'signal': 'Bear', 'date': '2020-01-11'}, 'code': 533122, 'company_name': 'RTNPOWER'},
{'pattern': {'signal': 'Bull', 'date': '2020-01-11'}, 'code': 539310, 'company_name': 'TISL'}, 
{'pattern': {'signal': 'Bull', 'date': '2020-01-11'}, 'code': 514183, 'company_name': 'BLACKROSE'} ]

It is the list of all the buying and selling signals for a particular stock. The above list has a dictionary of dictionary with the name as the pattern as the key. It contains signal and date as the key-value pair.  The question is how can you create a data frame with the column name as signal, date, code and company name. Here is the code.

rows = []
df1 = df["pattern"] # df is the dataframe for the above signal
for row,code,name in zip(df1,df["code"],df["company_name"]):
    row["code"] = code
    row["name"] = name
    rows.append(row)
rows
df2 = pd.DataFrame(rows)
df2

In the code above you can see first, I am extracting all dictionary items and iterating it with the code and name of the company stocks. After that, I am appending all the changes in the rows list. Then you can easily convert this list into DataFrames using pd.DataFrame() function. You will see the below output like this.

showing pattern code and company name

That’s all for now. I hope you have learned to Add Dictionary Keys and Values as Pandas Columns. If you have a query regarding this please contact us for more support.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner