How to convert dict of different lengths to dataframe

Python Pandas

In this, we will understand with code example how to convert dictionary of different lengths to dataframe.We can use pandas dataframe() constructor and form_dict() method.These methods can also be used to convert a nested dictionary to dataframe.

1. Dictionary of different length to Dataframe


In this example, we have the dictionary Student_dict. The nested dictionary Student_dict contain dictionary of different length. We are going to convert this dictionary content to a panda Dataframe. So let us understand this in steps.

  • We are iterating over the dictionary key, value pair using Student_dict.items() and creating pandas series of key-value pairs.
  • Converting the pandas series of key-value into a dictionary using dict().
  • Finally converting into dataframe using pandas dataframe constructor.

Program to convert dict of different length to Dataframe

#Python 3.X Program to convert Dictionary different length to Dataframe 
import pandas as pd
Student_dict = {'student1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'student2':{ 'Name': 'Rack', 'Mark': 100},
'student3':{ 'Name': 'Max', 'Subject': 'Music'},
'student4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}

df=pd.DataFrame(dict([ (k,pd.Series(v)) for k,v in Student_dict.items() ]))

print(df)
 

Output

       student1 student2 student3 student4
Mark         100      100      NaN      100
Name        Jack     Rack      Max    David
Subject     Math      NaN    Music     Math

2. Pandas form_dict() Method


In this second method, we will learn about the form_dict() method to convert the content of a dictionary to a Pandas Dataframe. The pd.DataFrame.from_dict() method takes a dictionary as an argument and converts it into dataFrame. It creates a dataframe that has default orientation which means that the keys of the dictionary are used as columns of dataframe and values as an index. We can change the orientation to ‘index ‘ which means index used as columns to dataframe and key as columns.

Let us first understand the syntax and then followed it with an example.

Syntax

pd.DataFrame.from_dict(dict,orient='columns',dtype=none) 

Convert dict of different length to Dataframe


In this example, we are using pd.DataFrame.from_dict() method to convert dict of different lengths to Dataframe this method takes dictionary and orient as an argument. The default orientation is columns, we can orient =‘index’ as per need.

Program to Convert dict of different length to Dataframe

#Python 3.X Program to convert Dictionary different length to Dataframe

import pandas as pd
Student_dict = {'student1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
 'student2':{ 'Name': 'Rack', 'Mark': 100},
'student3':{ 'Name': 'Max', 'Subject': 'Music'},
'student4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
 
}



df = pd.DataFrame.from_dict(Student_dict).transpose()

print(df)

Output

         Name Mark Subject
student1   Jack  100    Math
student2   Rack  100     NaN
student3    Max  NaN   Music
student4  David  100    Math

Summary

We have discussed how to convert dictionaries of different lengths to convert dict of different lengths to dataframe using two different ways iterating over the dictionary key, value pair using Student_dict.items() and Pandas form_dict() Method.