0
0
Pandasdata~5 mins

Creating DataFrame from list of dictionaries in Pandas

Choose your learning style9 modes available
Introduction

We use a DataFrame to organize data in rows and columns, like a table. Creating a DataFrame from a list of dictionaries helps turn messy data into a neat table quickly.

You have data collected as multiple records, each with named fields.
You want to analyze or visualize data stored as dictionaries.
You receive JSON-like data and want to convert it into a table.
You want to combine several small data entries into one table.
Syntax
Pandas
import pandas as pd

list_of_dicts = [
    {'column1': 'value1', 'column2': 'value2'},
    {'column1': 'value3', 'column2': 'value4'}
]

dataframe = pd.DataFrame(list_of_dicts)

Each dictionary in the list represents one row in the DataFrame.

Keys in dictionaries become column names in the DataFrame.

Examples
This creates a DataFrame with two rows and columns 'name' and 'age'.
Pandas
import pandas as pd

# Example 1: Normal case with two dictionaries
list_of_dicts = [
    {'name': 'Alice', 'age': 30},
    {'name': 'Bob', 'age': 25}
]
df = pd.DataFrame(list_of_dicts)
print(df)
When the list is empty, the DataFrame is empty with no rows or columns.
Pandas
import pandas as pd

# Example 2: Empty list
list_of_dicts = []
df = pd.DataFrame(list_of_dicts)
print(df)
DataFrame with one row is created.
Pandas
import pandas as pd

# Example 3: One dictionary only
list_of_dicts = [{'name': 'Charlie', 'age': 40}]
df = pd.DataFrame(list_of_dicts)
print(df)
Missing values appear as NaN in the DataFrame.
Pandas
import pandas as pd

# Example 4: Dictionaries with missing keys
list_of_dicts = [
    {'name': 'Dana', 'age': 22},
    {'name': 'Eli'}
]
df = pd.DataFrame(list_of_dicts)
print(df)
Sample Program

This program shows how to create a DataFrame from a list of dictionaries, print it, then add another dictionary and create a new DataFrame to see the update.

Pandas
import pandas as pd

# Create a list of dictionaries representing people
people_data = [
    {'name': 'Alice', 'age': 30, 'city': 'New York'},
    {'name': 'Bob', 'age': 25, 'city': 'Los Angeles'},
    {'name': 'Charlie', 'age': 35, 'city': 'Chicago'}
]

# Print original list
print('Original list of dictionaries:')
print(people_data)

# Create DataFrame from list of dictionaries
people_df = pd.DataFrame(people_data)

# Print DataFrame before any changes
print('\nDataFrame created from list of dictionaries:')
print(people_df)

# Add a new person to the list
people_data.append({'name': 'Dana', 'age': 28, 'city': 'Houston'})

# Create new DataFrame after adding
updated_people_df = pd.DataFrame(people_data)

# Print updated DataFrame
print('\nUpdated DataFrame after adding one more person:')
print(updated_people_df)
OutputSuccess
Important Notes

Time complexity is O(n) where n is the number of dictionaries, because each dictionary is processed once.

Space complexity is O(n*m) where m is average number of keys per dictionary, as data is stored in memory.

Common mistake: forgetting that missing keys in some dictionaries become NaN in the DataFrame.

Use this method when you have data naturally organized as records (dictionaries). For large datasets, consider reading from files or databases directly.

Summary

Creating a DataFrame from a list of dictionaries turns each dictionary into a row.

Keys in dictionaries become column names; missing keys show as NaN.

This is a quick way to organize and analyze structured data in Python.