How to Convert Python Dictionary to Pandas DataFrame

Do you want to convert a Python Dictionary to a Pandas DataFrame? In this Pandas tutorial, I will explain to you how to convert a Python dictionary to Pandas DataFrame using different methods with some examples.

Python Dictionary to Pandas DataFrame

There are 4 different ways to convert a Python dictionary to Pandas DataFrame.

  1. Convert using Pandas Constructor ( pd.dataframe() )
  2. Convert using from_dict() method.
  3. Convert using the from_records() method.
  4. Convert Python Dictionary to DataFrame with keys and a list of values with different lengths.

Let’s see them one by one using some demonstrative examples:

1. Python Dictionary to DataFrame using Pandas Constructor

We can convert Python Dictionary to DataFrame by using the Pandas Constructor method.

In the Pandas library, there is a predefined class called DataFrame, so we will use that class to convert Dictionary to DataFrame and that’s why this method is called Pandas Constructor.

Here is the code to convert Python Dictionary to DataFrame using Pandas Constructor.

Code :

import pandas as pd 
Employees = {'Name' : ['John', 'Frankline', 'James'] , 
        'Age': [20, 26, 63],
        'City' : ["London", 'Bristol' ,'Cardiff']}
Employees_df = pd.DataFrame(Employees)
print(Employees_df)

Output:

        Name  Age     City
0       John   20   London
1  Frankline   26  Bristol
2      James   63  Cardiff

Let me share a screenshot of a Practical Example using Pandas Constructor :

Python Dictionary to DataFrame using Pandas Constructor method

2. Convert Python Dictionary using from_dict() method

This from_dict() method in Python is a part of the Pandas Library and a convenient way to convert Python Dictionary to Pandas DataFrame.

This method is specifically defined for cases where data is organized in a dictionary structure.

Here is the code to convert Python Dictionary to DataFrame using the from_dict() method.

Code :

import pandas as pd
food_menu = {
    "pizza_type": ['Margerita', "Onion", "Paneer" , "Mashroom"],
    "price": [120, 180 , 150, 220],
    "is_available": [True, False, False, True]
}
food_menu_df = pd.DataFrame.from_dict(food_menu)
print(food_menu_df)

Output:

  pizza_type  price  is_available
0  Margerita    120          True
1      Onion    180         False
2     Paneer    150         False
3   Mashroom    220          True

Screenshot of Practical Example Using from_dict() method :

Convert Python Dictionary to DataFrame using from_dict()

Using Orient Parameter In from_dict() function to convert Python Dict to Pandas DataFrame
In this method, there is one parameter called orient, which is used to visualize the data in different ways. Let’s see one more example of the from_dict() method, where we will give an orient parameter.

Code:

import pandas as pd
food_menu = {
    "pizza_type": ['Margerita', "Onion", "Paneer" , "Mashroom"],
    "price": [120, 180 , 150, 220],
    "is_available": [True, False, False, True]
}
food_menu_df = pd.DataFrame.from_dict(food_menu, orient="index")
print(food_menu_df)

Output:

                      0      1       2         3
pizza_type    Margerita  Onion  Paneer  Mashroom
price               120    180     150       220
is_available       True  False   False      True

Image of Practical Example using orient parameter in from_dict() method.

How can i use orient method in Pandas

3. Convert Python Dict to DataFrame using from_records() method

This from_records() method is used to create a DataFrame from a sequence of records, where each record is either dictionary, list, or tuple. We will use this method to convert Python Dictionary to DataFrame.

Here is the code to convert Python Dictionary to Pandas DataFrame using the from_records() method.

import pandas as pd
Employee_data = [
    {'Name': 'Alice', 'Age': 25, 'City': 'New York'},
    {'Name': 'Bob', 'Age': 30, 'City': 'San Francisco'},
    {'Name': 'Charlie', 'Age': 22, 'City': 'Los Angeles'}
]
Employee_Data_df = pd.DataFrame.from_records(Employee_data)
print(Employee_Data_df)

Output:

      Name  Age           City
0    Alice   25       New York
1      Bob   30  San Francisco
2  Charlie   22    Los Angeles

Screenshot of the code and output :

How to convert Python Dictionary to Pandas DataFrame using from_record() method

4. Covert Python Dictionary to DataFrame with keys and list of values with different lengths

In case, when the length of the lists is uneven inside the dictionary, then we can use this method to handle dictionaries with uneven lengths of lists.

Here is the code to convert Python Dictionary to Pandas DataFrame when the length of the list is uneven.

Code :

import pandas as pd
Shop = {
	'soap': ['Ivory', 'Dove', 'Aveeno','Cetaphil'],
	'shampoo': ['Tresemme','Loreal',"Suave"],
	'toothpaste': ['Colgate', 'Parodontax', 'CloseUp','Oral-B']
}
Shop_df = pd.DataFrame(list(Shop.items()), columns=['Product', 'Brand'])
print(Shop_df)

Output:

      Product                                   Brand
0        soap         [Ivory, Dove, Aveeno, Cetaphil]
1     shampoo               [Tresemme, Loreal, Suave]
2  toothpaste  [Colgate, Parodontax, CloseUp, Oral-B]
Python Dictionary to DataFrame using Different length of lists

Conclusion

In this tutorial, I have explained how to convert how to convert Python Dict to Pandas DataFrame. These methods will convert your Python Dictionary to Pandas DataFrame, which is an important topic for Data Visualization, Data Manipulation, etc.

I have explained all 4 methods like pd.dataframe(), from_dict(), from_record(), etc that are used to convert Python Dictionary to Pandas DataFrame with practical examples.

You may like to read: