List of dict to Dataframe : Steps to Convert

A dictionary is a data structure that contains key-value pairs. If you consider more than one dictionary as an element of the list then it is a List of dict or dictionaries. In this post, you will learn how to convert a list of dict to dataframe with steps.

Steps to convert List of dict to Dataframe

Let’s learn all the steps to convert list of dict to dataframe.

Step 1: Import the pandas library

The first step is to import the pandas library. To import it use the import statement.

import pandas as pd

Step 2: Create a List of Dictionaries

Create a sample list of dictionaries that will assigned to the variable data. It will be used for the conversion.

data = [
    {"Name": "John", "Age": 28, "City": "New York"},
    {"Name": "Maya", "Age": 32, "City": "California"},
    {"Name": "Bob", "Age": 45, "City": "Chicago"}
]

Step 3: Use the pd.DataFrame() constructor

Now after the creation of the data you can convert list of dict to dataframe using the pd.DataFrame() constructor. Just pass the data as an argument.

df = pd.DataFrame(data)

Step 4: Print the dataframe

Optionally, you can print the resulting DataFrame to see the converted data.

print(df)

Full Code

import pandas as pd
data = [
    {"Name": "John", "Age": 28, "City": "New York"},
    {"Name": "Maya", "Age": 32, "City": "California"},
    {"Name": "Bob", "Age": 45, "City": "Chicago"}
]
df = pd.DataFrame(data)
print(df)

Output

Name  Age        City
0  John   28    New York
1  Maya   32  California
2   Bob   45     Chicago

Conclusion

In this post, you have learned how to create a sample data that is a list of dictionaries. You also learned how to convert it into dataframe using the pd.DataFrame() constructor. Just follow the steps for more understanding.

Hi, I am CodeTheBest. Here you will learn the best coding tutorials on the latest technologies like a flutter, react js, python, Julia, and many more in a single place.

SPECIAL OFFER!

This Offer is Limited! Grab your Discount!
15000 ChatGPT Prompts
Offer Expires In:
close-link