Convert a Dictionary to a Pandas Dataframe in Python

Here, we learn how to conversion of Python Dictionaries to Pandas.

Dictionary to Pandas Dataframe

Here First, you need to understand the basics of the Python dictionary and the Pandas library functions.

  • dictionary stores values ​​for various variables in key-value format. A dictionary is a kind of mutable data type, that collects them in an ordered mutable format and does not allow duplication.
  • Pandas is for a working database which is a python Library. It has the ability to analyze, clean, explore, and manipulate data. Pandas allow you to analyze big data and draw conclusions based on statistical theory. The Pandas can remove messy data and make them more easily readable and relevant for manipulation.
import pandas as pd

There is one of the most common tasks when working with Python and Pandas is converting a dictionary to a data frame. In this article, we explore different ways to convert a Python dictionary to a Pandas DataFrame, depending on the structure of the data and how it was originally stored. We can convert the dictionary to a Pandas data frame in Python by using this method.

pd.DataFrame.from_dict()

Sample code:-

import pandas as pd

# Create dict object
student_data = {
  'fist_name':['Johny', 'Andrew', 'Maria', 'Helen'],
  'last_name':['Burete', 'Daderio', 'Romani', 'Luman'],
  'color tone':['White','Black','Fair','Brown'],
  'age':[28, 45, 78, 29],
  'Location':['Georgia','Germany','Amsterdam','Britain']
}
# Create DataFrame from dict
student_dt = pd.DataFrame(student_data)
print(student_dt)

Sample Output:-

  first_name last_name  color  tone  age Location
0   Johny    Burete     White   28    Georgia
1   Andrew   Daderio    Black   45    Germany
2   Maria    Romani      Fair   78   Amsterdam
3   Helen    Luman      Brown   29    Britain

Reference

For more information, use the following link to learn how to convert a dictionary to a pandas dataframe in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *