Convert list of dictionaries to DataFrame [Practical Examples]

Convert list of dictionaries to DataFrame [Practical Examples]

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.

python
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:

  1. Using pandas.DataFrame()
  2. Using pandas.DataFrame() with index
  3. Using pandas.DataFrame() with index and columns
  4. Using pandas.DataFrame() with from_records() function
  5. Using pandas.DataFrame() with from_records() function with index
  6. Using pandas.DataFrame() with from_records() function with index and columns
  7. Using pandas.DataFrame() with from_dict() function
  8. 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:

python
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.

python
# 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:

python
pandas.DataFrame(list_of_dictionaries,index)

where,

  1. list_of_dictionaries is the input list of dictionaries
  2. index is to provide the index labels in a list

Example 1:Python program to pass list of dictionaries to a dataframe with indices.

python
# 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.

python
# 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:

python
pandas.DataFrame(list_of_dictionaries,index,columns)

where,

  1. list_of_dictionaries is the input list of dictionaries
  2. index is to provide the index labels in a list
  3. columns 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.

python
# 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:

python
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()

python
# 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:

python
pandas.DataFrame.from_records(list_of_dictionaries,index)

where,

  1. list_of_dictionaries is the input list of dictionaries
  2. index is to provide the index labels in a list

Example 1:Python program to pass list of dictionaries to a dataframe with indices.

python
# 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

python
# 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:

python
pandas.DataFrame.from_records(list_of_dictionaries,index,columns)

where,

  1. list_of_dictionaries is the input list of dictionaries
  2. index is to provide the index labels in a list
  3. columns 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.

python
# 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:

python
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()

python
# 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:

python
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()

python
# 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

Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with over a decade of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, …

  • Red Hat Certified System Administrator in Red Hat OpenStack
  • Certified Kubernetes Application Developer (CKAD)
  • Red Hat Certified Specialist in Ansible Automation
  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security