How to create DataFrame from dictionary in Python ?
Create dataframe from dictionary pandas: In this article we will discuss about various ways of creating DataFrame object from dictionaries.
So, let’s start exploring different approaches to achieve this result.
- Create DataFrame from Dictionary using default Constructor
- Create DataFrame from Dictionary with custom indexes
- Create DataFrame from Dictionary and skip data
- Create DataFrame from Dictionary with different Orientation
- Create DataFrame from nested Dictionary
Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.
Method-1 : Create DataFrame from Dictionary using default Constructor :
Create dataframe from dictionary: In python DataFrame constructor accepts n-D array, dictionaries etc.
Syntax : pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)
#program : # import pandas library import pandas as pd # dictionary with list object in values data = { 'Name' : ['Satya', 'Omm', 'Rakesh'], 'Age' : [21, 21, 23], 'From' : ['BBSR', 'RKL', 'KDP'] } # creating a Dataframe object df_obj = pd.DataFrame(data) df_obj
Output : Name Age From 0 Satya 21 BBSR 1 Omm 21 RKL 2 Rakesh 23 KDP
All keys in dictionary are converted to column name and values in dictionaries converted column data.
- Python: Find indexes of an element in pandas dataframe | Python Pandas Index.get_loc()
- Python Pandas DataFrame sub() Function
- Append/Add Row to Dataframe in Pandas – dataframe.append() | How to Insert Rows to Pandas Dataframe?
Method-2 : Create DataFrame from Dictionary with custom indexes :
#program : # import pandas library import pandas as pd # dictionary with list object in values data = { 'Name' : ['Satya', 'Omm', 'Rakesh'], 'Age' : [21, 21, 23], 'From' : ['BBSR', 'RKL', 'KDP'] } # creating a Dataframe object df_obj = pd.DataFrame(data, index = ['a','b','c']) df_obj
Output : Name Age From a Satya 21 BBSR b Omm 21 RKL c Rakesh 23 KDP
By passing index list, we can avoid the default index.
Method-3 : Create DataFrame from Dictionary and skip data
Pandas create dataframe from dictionary: By skipping some of the items of dictionary, we can also create a DataFrame object from dictionary
Let’s see the implementation of that.
#program : # import pandas library import pandas as pd # dictionary with list object in values data = { 'Name' : ['Satya', 'Omm', 'Rakesh'], 'Age' : [21, 21, 23], 'From' : ['BBSR', 'RKL', 'KDP'] } # creating a Dataframe object #items skipped with key 'Age' df_obj = pd.DataFrame(data, columns=['name', 'From']) df_obj
Output : Name From 0 Satya BBSR 1 Omm RKL 2 Rakesh KDP
Method-4 : Create DataFrame from Dictionary with different Orientation
Dataframe from dictionary: DataFrame can also be created from dictionary using DataFrame.from_dict()
function.
DataFrame.from_dict(data, orient='columns', dtype=None)
It accepts orientation too, where we can pass the orientation as index then the keys which were used as columns during creating DataFrame now they will be used as index.
Let’s see the implementation of that.
#program : # import pandas library import pandas as pd # dictionary with list object in values data = { 'Name' : ['Satya', 'Omm', 'Rakesh'], 'Age' : [21, 21, 23], 'From' : ['BBSR', 'RKL', 'KDP'] } # creating a Dataframe object #items skipped with key 'Age' df_obj = pd.DataFrame(data,orient='index') df_obj
Output : 0 1 2 Aame Satya Omm Rakesh From BBSR RKL KDP Age 21 21 23
Method-5 : Create DataFrame from nested Dictionary :
Python dict to dataframe: Suppose we have a nested dictionary, then we ill directly pass it in DataFrame constructor where the keys of dictionary will be used as column.
Let’s see the implementation of that.
#program : # import pandas library import pandas as pd # dictionary with list object in values # Nested Dictionary data = { 0 : { 'Name' : 'Satya', 'Age' : 21, 'From' : 'BBSR' }, 1 : { 'Name' : 'Omm', 'Age' : 21, 'From' : 'RKL' }, 2 : { 'Name' : 'Rakesh', 'Age' : 23, 'From' : 'KDP' } } # creating a Dataframe object #items skipped with key 'Age' df_obj = pd.DataFrame(data) df_obj
Output : 0 1 2 Aame Satya Omm Rakesh From BBSR RKL KDP Age 21 21 23
Read more Articles on Python Data Analysis Using Padas – Creating Dataframe Objects: