Categories: TutorialsUbuntu

How to Convert Python Dictionary into Dataframe

Python features dictionary data structure that allows you to easily store data as key-value pairs in a compact manner. It also offers pandas library that allows you to analyze data in tabular manner. Often we need to convert Python dictionary into dataframe, especially if we use python for server programming and receive JSON data from client browsers that is converted into python dictionary before processing. In this article, we will learn how to convert python dictionary into dataframe.

How to Convert Python Dictionary into Dataframe

Let us say you have a python dictionary as shown below where each key is a date and value is a sale amount.

d = {u'2012-06-08': 388,
 u'2022-06-09': 388,
 u'2022-06-10': 388,
 u'2022-06-11': 389,
 u'2022-06-12': 389,
 u'2022-06-13': 389,
 u'2022-06-14': 389,
 u'2022-06-15': 389,
...
}

Let us say you want to convert this dictionary into dataframe as shown below.

     Date         DateValue
0    2022-06-08    388
1    2022-06-09    388
2    2022-06-10    388
.    2022-06-11    389
.    ...           ...

You can easily convert this into dataframe as shown below using DataFrame() constructor. Please note, you need to pass a list of key-value pairs as input to DataFrame() constructor.

In [11]: pd.DataFrame(d.items())  # or list(d.items()) in python 3
Out[11]:
             0    1
0   2022-06-08  388
1   2022-06-09  388
2   2022-06-10  388
3   2022-06-11  389
...

In [12]: pd.DataFrame(d.items(), columns=['Date', 'DateValue'])
Out[12]:
          Date  DateValue
0   2022-06-08        388
1   2022-06-09        388
2   2022-06-10        388

In this article, we have learnt how to convert python dictionary to dataframe. It is useful if you want to analyze python dictionary using pandas framework.

Also read:

How to Start Background Process in Python
How to Prevent NGINX from Serving .git directory
How to Prevent Apache from Serving .git directory
How to Check if String is Substring of List Items
How to Check if Column is Empty or Null in MySQL

The post How to Convert Python Dictionary into Dataframe appeared first on Fedingo.

Ubuntu Server Admin

Recent Posts

Advantech AOM-2721 is now Ubuntu Certified

Canonical announces that the Advantech AOM-2721 is officially joining the list of Ubuntu Certified Hardware.…

8 hours ago

Ruby and Rails Performance Roundup: The Backlog Edition

Yet again instead of tweets, a blog post. The backlog got out of hand -…

8 hours ago

How to Deploy OpenProject on Ubuntu VPS

This article provides a guide demonstrating how to deploy OpenProject on Ubuntu VPS. What is…

9 hours ago

Canonical integrates NVIDIA Nemotron 3.5 Lightning with Ubuntu for always-on AI agents

Canonical is pleased to announce that  NVIDIA’s newly introduced NVIDIA Nemotron 3.5 Lightning, an open,…

2 days ago

Xfwl4’s Second Preview Release

I’m pleased to announce the second preview release of xfwl4, Xfce’s Wayland compositor, version 4.21.1.…

2 days ago

How to Install, Secure, and Set Up MySQL on Ubuntu 26.04 LTS

If you have been using an ecommerce store to place online orders or have been…

3 days ago