Convert list of dictionaries to DataFrame [Practical Examples]


Deepak Prasad

Python Pandas

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:

  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:

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,

  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.

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

  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.

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

  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.

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

  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.

# 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

 

Views: 6

Deepak Prasad

He is the founder of GoLinuxCloud and brings 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 in various domains, from development to DevOps, Networking, and Security, ensuring robust and efficient solutions for diverse projects. You can reach out to him on his LinkedIn profile or join on Facebook page.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment

GoLinuxCloud Logo


We try to offer easy-to-follow guides and tips on various topics such as Linux, Cloud Computing, Programming Languages, Ethical Hacking and much more.

Programming Languages

JavaScript

Python

Golang

Node.js

Java

Laravel