0
0
Pandasdata~10 mins

Creating DataFrame from list of dictionaries in Pandas - Visual Walkthrough

Choose your learning style9 modes available
Concept Flow - Creating DataFrame from list of dictionaries
Start with list of dictionaries
Pass list to pd.DataFrame()
pandas reads each dictionary
Each dict becomes a row
Keys become columns
DataFrame created with rows and columns
We start with a list of dictionaries, then pandas reads each dictionary as a row, using keys as columns, and creates the DataFrame.
Execution Sample
Pandas
import pandas as pd

list_of_dicts = [
    {'name': 'Alice', 'age': 25},
    {'name': 'Bob', 'age': 30},
    {'name': 'Charlie', 'age': 35}
]

df = pd.DataFrame(list_of_dicts)
print(df)
This code creates a DataFrame from a list of dictionaries where each dictionary is a row.
Execution Table
StepActionInputResulting DataFrame RowsColumns
1Start with list of dictionaries[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]None yetNone yet
2Pass list to pd.DataFrame()list_of_dictsRows created for each dictColumns set from keys: 'name', 'age'
3DataFrame constructedDataFrame objectRow 0: {'name': 'Alice', 'age': 25} Row 1: {'name': 'Bob', 'age': 30} Row 2: {'name': 'Charlie', 'age': 35}Columns: 'name', 'age'
4Print DataFramedf name age 0 Alice 25 1 Bob 30 2 Charlie 35Columns: 'name', 'age'
💡 All dictionaries converted to rows; DataFrame ready with columns from keys.
Variable Tracker
VariableStartAfter pd.DataFrame()Final
list_of_dicts[{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'age': 30}, {'name': 'Charlie', 'age': 35}]Same listSame list
dfNot definedDataFrame with 3 rows and 2 columnsDataFrame with 3 rows and 2 columns
Key Moments - 2 Insights
Why do the keys of dictionaries become columns in the DataFrame?
Because pandas treats each dictionary as a row and uses the keys as column names, as shown in execution_table step 3.
What happens if dictionaries have different keys?
pandas will create columns for all keys found; missing values in some rows become NaN. This is not shown here but is important to know.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 3, how many rows does the DataFrame have?
A2
B1
C3
D0
💡 Hint
Check the 'Resulting DataFrame Rows' column at step 3 in the execution_table.
At which step does the DataFrame get its columns defined?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Columns' column in execution_table rows to see when columns appear.
If one dictionary lacked the 'age' key, what would happen in the DataFrame?
AThe missing value becomes NaN in that row
BAn error occurs
CThe column is removed
DThe row is skipped
💡 Hint
Recall pandas behavior with missing keys in dictionaries when creating DataFrames.
Concept Snapshot
Creating DataFrame from list of dictionaries:
- Use pd.DataFrame(list_of_dicts)
- Each dict becomes a row
- Keys become columns
- Missing keys result in NaN
- Easy way to build tables from structured data
Full Transcript
We start with a list of dictionaries, each dictionary representing a row with key-value pairs. Passing this list to pandas DataFrame constructor creates a table where keys become columns and each dictionary is a row. The DataFrame then can be printed or used for analysis. If dictionaries have different keys, pandas fills missing values with NaN. This method is simple and common for creating DataFrames from structured data.