Open In App

How to convert Dictionary to Pandas Dataframe?

Last Updated : 05 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Converting a dictionary into a Pandas DataFrame is simple and effective. You can easily convert a dictionary with key-value pairs into a tabular format for easy data analysis. Lets see how we can do it using various methods in Pandas.

1. Using the Pandas Constructor

We can convert a dictionary into DataFrame using pd.DataFrame() function. In this method each key in the dictionary becomes a column label and the corresponding values forms data in those columns.

Python
import pandas as pd
data = {'name': ['Ansh', 'Sahil', 'Hardik', 'Nandini'],'age': ['22', '21', '23', '20']}
data = pd.DataFrame.from_dict(data)
print(data)

Output:

dict1
Using the Pandas

2. Using from_dict() Method

The pd.DataFrame.from_dict() method provides more flexibility and allows us to specify orientation of DataFrame using the orient parameter. This method helps us convert data into a dictionary-like format and control its structure.

Python
import pandas as pd
data = {'area': ['new delhi', 'kolkata', 'mumbai'],'rainfall': [90, 110, 200],'temperature': [40, 35, 29]}
df = pd.DataFrame.from_dict(data, orient='index')
print(df)

Output:

dict2
Using from_dict() Method

3. Handling Unequal Lengths in Dictionary

When dictionaries have the lists with unequal lengths directly converting them to a DataFrame can lead to errors. In such cases we can first convert each key-value pair into separate Series and then combine them into a DataFrame. This method helps when dealing with inconsistent data.

Python
import pandas as pd
data = {'key1': [1, 2, 3],'key2': [4, 5],'key3': [6, 7, 8, 9]}
df = pd.DataFrame(list(data.items()), columns=['Key', 'Values'])
print(df)

Output:

dict3
Handling Unequal Lengths in Dictionary

As we continue working with Pandas these methods help us efficiently convert dictionaries into DataFrames.


Next Article
Practice Tags :

Similar Reads