In this post, we are going to learn how to convert Dictionary to Pandas Dataframe using multiple ways that include a single dictionary, nested dictionary, Dictionary of different lengths, list of dictionaries, Dictionary key as columns.
1. Python Dictionary to Dataframe using constructor
In the pd.DataFrame() constructor the dictionary(Student_dict.items()) is passed as an argument where dictionary key will be columns and value will be index.We have specified custom index [‘Row_1′,’Row_2′,’Row_3′,’Row_4’].
If we only pass dictionary names it will not raise value error.
Program Example
import pandas as pd
Student_dict = {'Jack': 100, 'Rack': 100,
'Max': 100,
'David':100
}
dict_to_list = list(Student_dict.items())
dict_to_df = pd.DataFrame(dict_to_list,columns=['Name','Marks'],index=['Row_1','Row_2','Row_3','Row_4'])
print(dict_to_df)
Output
Name Marks
Row_1 Jack 100
Row_2 Rack 100
Row_3 Max 100
Row_4 David 100
- How to iterate over nested dictionary in Python
- 5 ways to Remove duplicates list of Python dictionaries
- Filter a list of Python dictionaries by conditions
- How To Convert Dict Of Different Lengths To Dataframe
2. Dictionary to dataframe keys as columns
The Student_dict has a list of values corresponding to a single key. We are passing a dictionary as an argument to the dataframe constructor and specified the index too. The created data frame will have dictionary keys as columns and values as indexes.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
dict_to_df = pd.DataFrame(Student_dict,index=['Row_1','Row_2','Row_3','Row_4'])
print(dict_to_df)
Output
Name Marks Subject
Row_1 Jack 100 Math
Row_2 Rack 100 Math
Row_3 Max 100 Music
Row_4 David 100 Physic
3. Nested dictionaries to Pandas DataFrame
We have a nested dictionary that we have passed as an argument to the dataframe constructor. We have used the transpose() method to transpose columns wise to rows wise.
Program Example
import pandas as pd
Student_dict = {'student1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
'student2':{ 'Name': 'Rack', 'Mark': 100,'Subject': 'Math'},
'student3':{ 'Name': 'Max', 'Mark': 100,'Subject': 'Music'},
'student4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
}
dict_to_df = pd.DataFrame(Student_dict)
print(dict_to_df.transpose())
Output
Name Mark Subject
student1 Jack 100 Math
student2 Rack 100 Math
student3 Max 100 Music
student4 David 100 Math
4. Pandas form_dict() Method
The pd.DataFrame.from_dict() takes a dictionary as an argument and converts it into dataFrame. It creates a dataframe that has default orientation which is columns that mean keys of the dictionary is 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.
Syntax
pd.DataFrame.from_dict(dict,orient='columns',dtype=none)
5. Dictionary to Dataframe Using different orientation
We are converting nested dictionary Student_dict. By using pd.DataFrame.from_dict() method into a data frame by specifying the orientation by orient=’Index’.The default orientation is ‘columns’ which means the key will be used as a column.
Program Example
#python 3.x program dictionary to dataframe of using orientation
import pandas as pd
Student_dict = {'student1':{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
'student2':{ 'Name': 'Rack', 'Mark': 100,'Subject': 'Math'},
'student3':{ 'Name': 'Max', 'Mark': 100,'Subject': 'Music'},
'student4':{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
}
dict_to_df = pd.DataFrame.from_dict(Student_dict,orient='Index')
print(dict_to_df)
Output
Name Mark Subject
student1 Jack 100 Math
student2 Rack 100 Math
student3 Max 100 Music
student4 David 100 Math
6. List of dictionaries to Dataframe
In pd.DataFrame.from_dict() method we have passed List of dictionaries as an argument and the default orientation is columns that mean dictionary keys will be used as columns and values as an index.
Program Example
import pandas as pd
Student_dict = [{'Name': 'Jack','Mark': 100, 'Subject': 'Math'},
{ 'Name': 'Rack', 'Mark': 100,'Subject': 'Math'},
{ 'Name': 'Max', 'Mark': 100,'Subject': 'Music'},
{ 'Name': 'David', 'Mark':100,'Subject': 'Math'}
]
dict_to_df = pd.DataFrame.from_dict(Student_dict)
print(dict_to_df)
Output
Name Mark Subject
0 Jack 100 Math
1 Rack 100 Math
2 Max 100 Music
3 David 100 Math
7. Dictionary of different length to Dataframe
The nested dictionary Student_dict contain dictionary of different length.
- 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 Dictionary 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
8.Dictionary to dataframe by skip data
We can skip some columns while creating a dataframe from the dictionary. In this example, we have to skip the ‘Subject’ column.
Program Example
import pandas as pd
Student_dict = {
'Name': ['Jack', 'Rack', 'Max', 'David'],
'Marks':[100,100, 100,100],
'Subject': ['Math', 'Math', 'Music', 'Physic']
}
dict_to_df = pd.DataFrame(Student_dict,columns=['Name','Marks'])
print(dict_to_df)
Output
Name Marks
0 Jack 100
1 Rack 100
2 Max 100
3 David 100
Summary
We have explored multiple ways to Python Dictionary to Pandas Dataframe single, nested, list of dictionaries and dictionary of the list with examples.