How to create DataFrame from dictionary in Python-Pandas?
The task of converting a dictionary into a Pandas DataFrame involves transforming a dictionary into a structured, tabular format where keys represent column names or row indexes and values represent the corresponding data.
Using Default Constructor
This is the simplest method where a dictionary is directly passed to pd.DataFrame(). Here, dictionary keys become column names and values become the corresponding data.
import pandas as pd
d = {
'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'City': ['New York', 'London', 'Paris']
}
# creating a Dataframe object
df = pd.DataFrame(d)
print(df)
Output
Name Age City 0 Alice 25 New York 1 Bob 30 London 2 Charlie 35 Paris
Table of Content
Using Custom Indexes
By default, Pandas assigns numerical row indexes (0,1,2,...). however we can define custom indexes using the index parameter.
import pandas as pd
# dictionary with list object in values
d = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Age' : [23, 21, 22, 21],
'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
# creating a Dataframe object from dictionary with custom indexing
df = pd.DataFrame(d, index = ['a', 'b', 'c', 'd'])
print(df)
Output
Name Age University a Ankit 23 BHU b Aishwarya 21 JNU c Shaurya 22 DU d Shivangi 21 BHU
Using Simple Dictionary
When the dictionary contains only key-value pairs (instead of lists), we need to convert it into a tabular format using pd.DataFrame(list(dictionary.items())).
import pandas as pd
d = {
'Ankit' : 22,
'Golu' : 21,
'hacker' : 23
}
# creating a Dataframe object from a list of tuples of key, value pair
df = pd.DataFrame(list(d.items()))
print(df)
Output
0 1 0 Ankit 22 1 Golu 21 2 hacker 23
Selecting Specific Columns
We can create a DataFrame using only the required columns from the dictionary.
import pandas as pd
d = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Age' : [23, 21, 22, 21],
'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
# creating a Dataframe object with skipping one column i.e skipping age column.
df = pd.DataFrame(d, columns = ['Name', 'University'])
print(df)
Output
Name University 0 Ankit BHU 1 Aishwarya JNU 2 Shaurya DU 3 Shivangi BHU
Using Different Orientation (Keys as Indexes)
By default, dictionary keys act as column names, but we can use them as row indexes by setting orient='index'.
import pandas as pd
# dictionary with list object in values
d = {
'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],
'Age' : [23, 21, 22, 21],
'University' : ['BHU', 'JNU', 'DU', 'BHU'],
}
# creating a Dataframe object in which dictionary
# key is act as index value and column value is
# 0, 1, 2...
df = pd.DataFrame.from_dict(d, orient = 'index')
print(df)
Output
0 1 2 3 Name Ankit Aishwarya Shaurya Shivangi Age 23 21 22 21 University BHU JNU DU BHU