How to convert Dictionary to Pandas Dataframe?



Pandas is a popular library for data manipulation and analysis in Python. It provides various data structures such as Series, Dataframe, and Panel. In this article, we will discuss how to convert a dictionary to a Pandas Dataframe.

Using the from_dict() Method

The easiest way to convert a dictionary to a Pandas Dataframe is by using the from_dict() method. This method creates a Dataframe from a dictionary.

import pandas as pd

my_dict = {'name': ['John', 'Mary', 'Peter', 'Jane'],
           'age': [25, 30, 35, 40],
           'country': ['USA', 'Canada', 'UK', 'Australia']}

df = pd.DataFrame.from_dict(my_dict)

print(df)

[Output]

    name  age    country
0   John   25        USA
1   Mary   30     Canada
2  Peter   35         UK
3   Jane   40  Australia

As you can see, the from_dict() method creates a Dataframe from the dictionary. The keys of the dictionary become the column names, and the values become the values in the Dataframe.

Using the DataFrame() Constructor

We can also use the DataFrame() constructor to create a Pandas Dataframe from a dictionary. This method allows us to specify the column names and index labels of the Dataframe.

import pandas as pd

my_dict = {'name': ['John', 'Mary', 'Peter', 'Jane'],
           'age': [25, 30, 35, 40],
           'country': ['USA', 'Canada', 'UK', 'Australia']}

df = pd.DataFrame(data=my_dict, columns=['name', 'age', 'country'],
                   index=['a', 'b', 'c', 'd'])

print(df)

[Output]

    name  age    country
a   John   25        USA
b   Mary   30     Canada
c  Peter   35         UK
d   Jane   40  Australia

As you can see, we have specified the column names and index labels in the DataFrame() constructor. This method allows us to customize the Dataframe according to our requirements.

Using the pd.json_normalize() method

pd.json_normalize() method is a powerful method that can be used to normalize semi-structured JSON data into a flat table. However, it can also be used to convert a dictionary into a pandas dataframe.

import pandas as pd
# Creating dictionary

dictionary = {'Name': ['John', 'Smith', 'Anna', 'Jake'],
'Age': [34, 28, 29, 42],
'Gender': ['M', 'M', 'F', 'M']}
# Converting dictionary to pandas dataframe

df = pd.json_normalize(dictionary)
# Displaying dataframe

print(df)

Output:

   Name  Age Gender
0  John   34      M
1 Smith   28      M
2  Anna   29      F
3  Jake   42      M

Using the pd.DataFrame() constructor and Transposing

import pandas as pd
# Creating dictionary

dictionary = {'Name': ['John', 'Smith', 'Anna', 'Jake'],
'Age': [34, 28, 29, 42],
'Gender': ['M', 'M', 'F', 'M']}
# Converting dictionary to pandas dataframe

df = pd.DataFrame([dictionary])
# Transposing the dataframe

df = df.T.reset_index()
# Renaming the columns

df.columns = ['Feature', 'Value']
# Displaying dataframe

print(df)

Output:

  Feature  Value
0    Name   John
1     Age     34
2  Gender      M
3    Name  Smith
4     Age     28
5  Gender      M
6    Name   Anna
7     Age     29
8  Gender      F
9    Name   Jake
10    Age     42
11 Gender      M

Last Updated on May 16, 2023 by admin

Leave a Reply

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

Recommended Blogs