• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / List of Dictionaries to Dataframe in Python

List of Dictionaries to Dataframe in Python

Author: Aditya Raj
Last Updated: June 4, 2022

Dataframes are mainly used in python for the analysis of tabular data. In this article, we will discuss how we can convert a list of dictionaries to a dataframe in python.

List of Dictionaries to Dataframe Using pandas.DataFrame()

The dataframe objects are defined in the pandas module. To create a dataframe from a given list of dictionaries, we can use the DataFrame() method. The DataFrame() method object takes a list of dictionaries as input argument and returns a dataframe created from the dictionaries. Here, the column names for the dataframe consist of the keys in the python dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the pandas.DataFrame() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

In the dataframe, a column is created for each key in the list of dictionaries. If a dictionary doesn’t have any specific key, the value NaN is given to the column corresponding to the specific key in the row representing the dictionary.

List of Dictionaries to Dataframe in Python Using pandas.dataframe.from_dict() Method

Instead of using the DataFrame() method, we can also use the from_dict() method to convert a list of dictionaries to a dataframe in Python. The from_dict() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the from_dict() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame.from_dict(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

You can observe that the dataframe generated by the from_dict() method is similar to the dataframe generated by the DataFrame() method.

List of Dictionaries to Dataframe Using pandas.dataframe.from_records(Data)

To create a dataframe from a list of dictionaries in Python, we can also use the from_records() method. The from_records() method takes a list of dictionaries as its input argument and returns a dataframe. Again, the column names for the dataframe consist of the keys in the dictionary. The values of each dictionary are transformed into the rows of the dataframe. You can create a dataframe from a list of dictionaries using the from_records() method as follows.

import pandas as pd

listOfDict = [{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'},
              {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
print("THe list of dictionaries is:")
print(listOfDict)
df = pd.DataFrame.from_records(listOfDict)
print("The dataframe is:")
print(df)

Output:

THe list of dictionaries is:
[{'Name': 'Aditya', 'Roll': 1, 'Language': 'Python'}, {'Name': 'Sam', 'Roll': 2, 'Language': 'Java'}, {'Name': 'Chris', 'Roll': 3, 'Language': 'C++'}, {'Name': 'Joel', 'Roll': 4, 'Language': 'TypeScript'}]
The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript

Conclusion

In this article, we have discussed three ways to convert a list of dictionaries to a dataframe in python. All three methods are semantically similar and produce the same result. To learn more about dictionaries, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us