How to create DataFrame from dictionary in Python-Pandas? - GeeksforGeeks

10 Jul, 2020
Let’s discuss how to create DataFrame from dictionary in Pandas. There are multiple ways to do this task.
Method 1: Create DataFrame from Dictionary using default Constructor of pandas.Dataframe class.
Code:
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],    'Age' : [23, 21, 22, 21],    'University' : ['BHU', 'JNU', 'DU', 'BHU'],}  # creating a Dataframe object df = pd.DataFrame(details)  df
Output:
Method 2: Create DataFrame from Dictionary with user-defined indexes.
Code:
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    'Name' : ['Ankit', 'Aishwarya', 'Shaurya', 'Shivangi'],    'Age' : [23, 21, 22, 21],    'University' : ['BHU', 'JNU', 'DU', 'BHU'],}  # creating a Dataframe object from dictionary # with custom indexingdf = pd.DataFrame(details, index = ['a', 'b', 'c', 'd'])  df
Output:
Method 3: Create DataFrame from simple dictionary i.e dictionary with key and simple value like integer or string value.
Code:
# import pandas libraryimport pandas as pd  # dictionarydetails = {    'Ankit' : 22,    'Golu' : 21,    'hacker' : 23    }  # creating a Dataframe object from a list # of tuples of key, value pairdf = pd.DataFrame(list(details.items()))  df
Output:
Method 4: Create DataFrame from Dictionary with required columns only.
Code:
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    '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(details, columns = ['Name', 'University'])  df
Output:
Method 5: Create DataFrame from Dictionary with different Orientation i.e. Dictionary key is act as indexes in Dataframe.
Code:
# import pandas libraryimport pandas as pd  # dictionary with list object in valuesdetails = {    '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(details, orient = 'index')  df
Output:
Method 6: Create DataFrame from nested Dictionary.
Code:
# import pandas libraryimport pandas as pd  # dictionary with dictionary object# in values i.e. nested dictionarydetails = {     0 : {        'Name' : 'Ankit',        'Age' : 22,        'University' : 'BHU'        },    1 : {        'Name' : 'Aishwarya',        'Age' : 21,        'University' : 'JNU'        },    2 : {        'Name' : 'Shaurya',        'Age' : 23,        'University' : 'DU'        }}  # creating a Dataframe object# from nested dictionary# in which inside dictionary# key is act as index value# and column value is 0, 1, 2...df = pd.DataFrame(details)  # swap the columns with indexesdf = df.transpose()  df
Output:
Python pandas-dataFrame
Python-pandas
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Python Dictionary
Read a file line by line in Python
Enumerate() in Python
How to Install PIP on Windows ?
Iterate over a list in Python
Different ways to create Pandas Dataframe
Python program to convert a list to string
Reading and Writing to text files in Python
*args and **kwargs in Python
sum() function in Python