Convert List of Dictionaries to pandas DataFrame in Python (4 Examples)
Hi! This tutorial will show you four simple ways of converting a list of dictionaries into a pandas DataFrame in the Python programming language.
First, though, here is an overview of this tutorial:
Let’s jump right into it!
Install & Import pandas Library
If you do not have pandas already installed in and imported into your Python environment, run the lines of code below in your preferred Python programming IDE in order to install and import pandas; otherwise, you can skip to the next section:
# install pandas pip install pandas # import pandas import pandas as pd
Create List of Dictionaries
We will create a sample list of dictionaries using identical keys with differing values. Please run the code below.
list_of_dicts = [{"a": 1, "b": 2, "c": 3}, {"a": 3, "b": 4, "c": 5}, {"a": 6, "b": 7, "c": 8}]
As seen, list_of_dicts has three dictionaries, each of which has three key-value pairs with identical keys a, b and c.
Example 1: Create DataFrame from List of Dictionaries Using DataFrame() Constructor
In this example, we will use the pandas DataFrame()
function to convert the list of dictionaries into a pandas DataFrame by parsing the list to the function like so:
df = pd.DataFrame(list_of_dicts) print(df) # a b c #0 1 2 3 #1 3 4 5 #2 6 7 8 print(type(df)) # <class 'pandas.core.frame.DataFrame'>
In Python, we typically use the .
operator to call a function from a Python module. In this tutorial, the module has pd as an alias and the data type of df is tested via the type() function. As seen in the code chunk above, list_of_dicts was successfully converted into a DataFrame.
Example 2: Create DataFrame from List of Dictionaries Using DataFrame.from_dict() Function
In this second example, we will use the pandas DataFrame.from_dict()
function to convert the list of dictionaries into a pandas DataFrame by parsing the list to the function.
df = pd.DataFrame.from_dict(list_of_dicts) print(df) # a b c #0 1 2 3 #1 3 4 5 #2 6 7 8 print(type(df)) #<class 'pandas.core.frame.DataFrame'>
Just like in Example 1, df turned into a pandas Dataframe.
Example 3: Create DataFrame from List of Dictionaries Using DataFrame.from_records() Function
In this third example, we will use the pandas DataFrame.from_records()
function to convert the list of dictionaries into a pandas DataFrame by parsing the list to the function.
df = pd.DataFrame.from_records(list_of_dicts) print(df) # a b c #0 1 2 3 #1 3 4 5 #2 6 7 8 print(type(df)) #<class 'pandas.core.frame.DataFrame'>
You see, we got the same results with the previous examples.
Example 4: json_normalize() Function
In this fourth and final example, we will use the pandas json_normalize()
function to convert the list of dictionaries or a nested json into a pandas DataFrame by parsing the list to the function.
df = pd.json.normalize(list_of_dicts) print(df) # a b c #0 1 2 3 #1 3 4 5 #2 6 7 8 print(type(df)) #<class 'pandas.core.frame.DataFrame'>
With that, we have successfully demonstrated 4 simple methods of converting a list of dictionaries into a pandas DataFrame in the Python programming language. I hope you’ve found this helpful!
Video, Further Resources & Summary
Do you need more explanations on how to convert a list of dictionaries into a pandas DataFrame in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.
In the video, we explain in some more detail how to convert a list of dictionaries into a pandas DataFrame in Python.
The YouTube video will be added soon.
Furthermore, I urge you to check out other interesting Python list tutorials on Statistics Globe, starting with these ones:
- Check if String Exists in List in Python (3 Examples)
- Count Duplicates in List in Python (2 Examples)
- Access List Element by Index in Python (3 Examples)
- Check if List of Lists is Empty in Python (2 Examples)
- Learn Python Programming
This post has shown how to convert a list of dictionaries into a pandas DataFrame in Python. In case you have further questions, you may leave a comment below.
This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.
Statistics Globe Newsletter