In this tutorial we will discuss how to convert list of dictionaries to pandas DataFrame.
Different methods used to convert list of dictionaries to DataFrame
List of dictionaries means a set of dictionaries stored in a list separated by comma. So we are going to pass this list of dictionaries to the pandas DataFrame.
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
Methods Used:
- Using
pandas.DataFrame()
- Using pandas.DataFrame() with
index
- Using pandas.DataFrame() with
index and columns
- Using pandas.DataFrame() with
from_records()
function - Using pandas.DataFrame() with
from_records() function with index
- Using pandas.DataFrame() with
from_records() function with index and columns
- Using pandas.DataFrame() with
from_dict()
function - Using pandas.DataFrame() with
json_normalize()
function
Method 1 : Using pandas.DataFrame()
We are passing the list of dictionaries to the pandas dataframe using pandas.DataFrame()
Syntax:
pandas.DataFrame(list_of_dictionaries)
where, list_of_dictionaries
is the input list of dictionaries
Example: Python program to pass list of dictionaries to a dataframe.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame(book)
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
0 1 python 56 34
1 2 Node-js 45 54
2 3 R 100 80
3 4 Html 16 20
Method 2 : Using pandas.DataFrame() with index
We are passing the list of dictionaries to the pandas dataframe using pandas.DataFrame() with index labels
Syntax:
pandas.DataFrame(list_of_dictionaries,index)
where,
list_of_dictionaries
is the input list of dictionaries- index is to provide the
index labels
in a list
Example 1: Python program to pass list of dictionaries to a dataframe with indices.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame(book,index=[10,20,30,40])
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
10 1 python 56 34
20 2 Node-js 45 54
30 3 R 100 80
40 4 Html 16 20
We can also pass string variables as indices
Example 2: Python program to pass list of dictionaries to a dataframe with indices.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame(book,index=['A','B','C','D'])
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
A 1 python 56 34
B 2 Node-js 45 54
C 3 R 100 80
D 4 Html 16 20
Method 3 : Using pandas.DataFrame() with index and columns
We are passing the list of dictionaries to the pandas dataframe using pandas.DataFrame() with index labels and column names
Syntax:
pandas.DataFrame(list_of_dictionaries,index,columns)
where,
list_of_dictionaries
is the input list of dictionariesindex
is to provide the index labels in a listcolumns
are the column names to provide column names in a list
Example: Python program to pass list of dictionaries to a dataframe with index and columns.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame(book,index=[10,20,30,40],columns=['no_of_pages','price','name','id'])
# display dataframe
print(dataframe)
Output:
no_of_pages price name id
10 34 56 python 1
20 54 45 Node-js 2
30 80 100 R 3
40 20 16 Html 4
Method 4 : Using pandas.DataFrame() with from_records() function
Here we are using from_records()
function to pass list of dictionaries to the pandas dataframe.
Syntax:
pandas.DataFrame.from_records(list_of_dictionaries)
where, list_of_dictionaries is the input list of dictionaries
Example: Python program to pass list of dictionaries to the pandas dataframe with from_records()
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame.from_records(book)
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
0 1 python 56 34
1 2 Node-js 45 54
2 3 R 100 80
3 4 Html 16 20
Method 5 : Using pandas.DataFrame() with from_records() function with index
Here we are using from_records()
function to pass list of dictionaries to the pandas dataframe with index
.
Syntax:
pandas.DataFrame.from_records(list_of_dictionaries,index)
where,
list_of_dictionaries
is the input list of dictionaries- index is to provide the index labels in a list
Example 1: Python program to pass list of dictionaries to a dataframe with indices.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame.from_records(book,index=[10,20,30,40])
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
10 1 python 56 34
20 2 Node-js 45 54
30 3 R 100 80
40 4 Html 16 20
Example 2: We can also pass string variables as indices
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame.from_records(book,index=['A','B','C','D'])
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
A 1 python 56 34
B 2 Node-js 45 54
C 3 R 100 80
D 4 Html 16 20
Method 6 : Using pandas.DataFrame() with from_records() function with index and columns
We are passing the list of dictionaries to the pandas dataframe using pandas.DataFrame() with from_records()
function with index labels and column names
Syntax:
pandas.DataFrame.from_records(list_of_dictionaries,index,columns)
where,
list_of_dictionaries
is the input list of dictionariesindex
is to provide the index labels in a listcolumns
are the column names to provide column names in a list
Example: Python program to pass list of dictionaries to a dataframe with index and columns.
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame(book,index=[10,20,30,40],columns=['no_of_pages','price','name','id'])
# display dataframe
print(dataframe)
Output:
no_of_pages price name id
10 34 56 python 1
20 54 45 Node-js 2
30 80 100 R 3
40 20 16 Html 4
Method 7 : Using pandas.DataFrame() with from_dict() function
Here we are using from_dict()
function to pass list of dictionaries to the pandas dataframe.
Syntax:
pandas.DataFrame.from_dict(list_of_dictionaries)
where, list_of_dictionaries
is the input list of dictionaries
Example: Python program to pass list of dictionaries to the pandas dataframe with from_dict()
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.DataFrame.from_dict(book)
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
0 1 python 56 34
1 2 Node-js 45 54
2 3 R 100 80
3 4 Html 16 20
Method 8 : Using pandas.DataFrame() with json_normalize() function
Here we are using json_normalize()
function to pass list of dictionaries to the pandas dataframe.
Syntax:
pandas.json_normalize(list_of_dictionaries)
where, list_of_dictionaries is the input list of dictionaries
Example: Python program to pass list of dictionaries to the pandas dataframe with json_normalize()
# import the module
import pandas
# consider the food data in a list of dictionary
book = [{'id':1,'name':'python','price':56,'no_of_pages':34},
{'id':2,'name':'Node-js','price':45,'no_of_pages':54},
{'id':3,'name':'R','price':100,'no_of_pages':80},
{'id':4,'name':'Html','price':16,'no_of_pages':20}]
# pass this book to the dataframe
dataframe=pandas.json_normalize(book)
# display dataframe
print(dataframe)
Output:
id name price no_of_pages
0 1 python 56 34
1 2 Node-js 45 54
2 3 R 100 80
3 4 Html 16 20
Summary
In this tutorial , we discussed how to convert list of dictionaries to the pandas dataframe using pandas.DataFrame() method, from_records(),json_normalize() and from_dict()
functions. For two functions except from_dict() and json_normalize()
, we discussed index
and columns
parameter.
References