In the Python programming language, the dataframe is a popular data type to work with data tables in the data science field. In many cases, creating a dataframe from a list of dictionaries is necessary. Today, our article will give you some helpful knowledge about Python list of dictionaries to dataframe.
What is the list of dictionaries?
A dictionary is an object that has one or many pairs of keys and values. Visit our previous article to understand more about the list of dictionaries.
Python list of dictionaries to dataframe
The pandas is the most helpful library when working with data tables. It is easy to create a dataframe by calling pandas’ APIs. To create a dataframe from a list of dictionaries, we can use one of three following methods: DataFrame(), DataFrame.from_dict(), and DataFrame.from_records().
Below are detailed implementations for the methods.
Using pd.DataFrame()
The function creates a dataframe from an n-dimension array, dataframe, dictionary, or iterable object.
Code:
import pandas as pd data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"}, {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"}, {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"}, {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"}, {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}] # Create a dataframe from the list of dictionaries df = pd.DataFrame(data) print(df)
Result:
Name Age Hair_color State
0 John 7 Black Florida
1 Alex 8 Yellow Ohio
2 Brian 16 Black Texas
3 Peter 16 Yellow New York
4 Adam 13 Brown North Carolina
Using pd.DataFrame.from_dict()
The function creates a dataframe from a dictionary or sequence of dictionaries.
Code:
import pandas as pd data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"}, {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"}, {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"}, {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"}, {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}] # Create a dataframe from the list of dictionaries df = pd.DataFrame.from_dict(data) print(df)
Result:
Name Age Hair_color State
0 John 7 Black Florida
1 Alex 8 Yellow Ohio
2 Brian 16 Black Texas
3 Peter 16 Yellow New York
4 Adam 13 Brown North Carolina
Using pd.DataFrame.from_records()
The method creates a dataframe from a structured n – dimension array, dataframe, or sequence of dictionaries or tuples.
Code:
import pandas as pd data = [{"Name": "John", "Age": 7, "Hair_color": "Black", "State": "Florida"}, {"Name": "Alex", "Age": 8, "Hair_color": "Yellow", "State": "Ohio"}, {"Name": "Brian", "Age": 16, "Hair_color": "Black", "State": "Texas"}, {"Name": "Peter", "Age": 16, "Hair_color": "Yellow", "State": "New York"}, {"Name": "Adam", "Age": 13, "Hair_color": "Brown", "State": "North Carolina"}] # Create a dataframe from the list of dictionaries df = pd.DataFrame.from_records(data) print(df)
Result:
Name Age Hair_color State
0 John 7 Black Florida
1 Alex 8 Yellow Ohio
2 Brian 16 Black Texas
3 Peter 16 Yellow New York
4 Adam 13 Brown North Carolina
Summary
In summary, you can easily filter a list of dictionaries by flexibly accessing their keys and values. If the dictionaries have the same format of keys, access them with the keys. Else, access them by the items attribute.
Maybe you are interested:
- Differences between lists and dictionaries in python
- Python filter list of dictionaries
- Python lists of dictionaries

My name is Robert Collier. I graduated in IT at HUST university. My interest is learning programming languages; my strengths are Python, C, C++, and Machine Learning/Deep Learning/NLP. I will share all the knowledge I have through my articles. Hope you like them.
Name of the university: HUST
Major: IT
Programming Languages: Python, C, C++, Machine Learning/Deep Learning/NLP