Bird
Raised Fist0
Pandasdata~10 mins

Creating DataFrame from list of dictionaries in Pandas - Visual Walkthrough

Choose your learning style10 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.

Practice

(1/5)
1. What happens when you create a pandas DataFrame from a list of dictionaries?
easy
A. Each dictionary becomes a row, and keys become column names.
B. Each dictionary becomes a column, and keys become row labels.
C. The list is converted into a single column with dictionary objects.
D. The DataFrame will be empty because dictionaries cannot be converted.

Solution

  1. Step 1: Understand DataFrame creation from list of dicts

    Each dictionary in the list is treated as one row in the DataFrame.
  2. Step 2: Identify how keys are used

    Keys from dictionaries become the column names in the DataFrame.
  3. Final Answer:

    Each dictionary becomes a row, and keys become column names. -> Option A
  4. Quick Check:

    List of dicts -> rows, keys -> columns [OK]
Hint: Remember: dict keys become columns, dicts become rows [OK]
Common Mistakes:
  • Thinking dictionaries become columns instead of rows
  • Assuming keys become row labels
  • Believing the DataFrame will be empty
2. Which of the following is the correct syntax to create a DataFrame from a list of dictionaries named data?
easy
A. df = pd.DataFrame(data)
B. df = pd.DataFrame.from_list(data)
C. df = pd.DataFrame.create(data)
D. df = pd.DataFrame.list(data)

Solution

  1. Step 1: Recall pandas DataFrame constructor

    The correct way to create a DataFrame from data is using pd.DataFrame().
  2. Step 2: Check method names

    There is no from_list, create, or list method in pandas DataFrame.
  3. Final Answer:

    df = pd.DataFrame(data) -> Option A
  4. Quick Check:

    Use pd.DataFrame() constructor [OK]
Hint: Use pd.DataFrame(your_list) to create DataFrame [OK]
Common Mistakes:
  • Using non-existent methods like from_list or create
  • Confusing DataFrame constructor syntax
  • Missing pd prefix or parentheses
3. Given the code:
import pandas as pd
data = [{'name': 'Alice', 'age': 25}, {'name': 'Bob', 'city': 'NY'}]
df = pd.DataFrame(data)
print(df)

What will be the output?
medium
A. A DataFrame with columns 'name' and 'city' only.
B. A DataFrame with only columns 'name' and 'age', ignoring 'city'.
C. An error because dictionaries have different keys.
D. A DataFrame with columns 'name', 'age', 'city' and NaN where data is missing.

Solution

  1. Step 1: Check keys across dictionaries

    Keys are 'name', 'age' in first dict and 'name', 'city' in second dict.
  2. Step 2: Understand pandas behavior with missing keys

    Pandas creates columns for all keys found and fills missing values with NaN.
  3. Final Answer:

    DataFrame with columns 'name', 'age', 'city' and NaN where data is missing. -> Option D
  4. Quick Check:

    Missing keys -> NaN in DataFrame [OK]
Hint: Missing keys become NaN in DataFrame columns [OK]
Common Mistakes:
  • Assuming missing keys cause errors
  • Thinking columns are limited to first dict keys
  • Ignoring NaN values for missing data
4. What is wrong with this code snippet?
import pandas as pd
data = [{'a': 1, 'b': 2}, {'a': 3, 'c': 4}]
df = pd.DataFrame(data, columns=['a', 'b', 'c', 'd'])
print(df)
medium
A. DataFrame will only include columns 'a' and 'b'.
B. No error; columns not in data will be filled with NaN.
C. Error because columns list length does not match dictionaries.
D. Error because column 'd' does not exist in any dictionary.

Solution

  1. Step 1: Understand columns parameter effect

    Specifying columns includes those columns in DataFrame, even if missing in data.
  2. Step 2: Check missing columns behavior

    Columns not found in dictionaries are filled with NaN, no error occurs.
  3. Final Answer:

    No error; columns not in data will be filled with NaN. -> Option B
  4. Quick Check:

    Extra columns -> NaN, no error [OK]
Hint: Extra columns in list become NaN, no error [OK]
Common Mistakes:
  • Expecting error for missing columns
  • Thinking columns list must match keys exactly
  • Assuming DataFrame excludes columns not in data
5. You have a list of dictionaries representing sales data:
sales = [
  {'product': 'A', 'units': 10, 'price': 2.5},
  {'product': 'B', 'units': 5},
  {'product': 'A', 'price': 2.5},
  {'product': 'C', 'units': 7, 'price': 3.0}
]

You want to create a DataFrame and add a new column 'total' which is units * price, treating missing values as zero. Which code correctly does this?
hard
A. df = pd.DataFrame(sales) df['total'] = df['units'].fillna(0) + df['price'].fillna(0) print(df)
B. df = pd.DataFrame(sales) df['total'] = df['units'] * df['price'] print(df)
C. df = pd.DataFrame(sales) df['units'] = df['units'].fillna(0) df['price'] = df['price'].fillna(0) df['total'] = df['units'] * df['price'] print(df)
D. df = pd.DataFrame(sales) df['total'] = df['units'] * df['price'] df.fillna(0) print(df)

Solution

  1. Step 1: Create DataFrame from list of dicts

    Use pd.DataFrame(sales) to convert list to DataFrame with NaNs for missing keys.
  2. Step 2: Replace NaN with 0 before multiplication

    Fill missing 'units' and 'price' with 0 to avoid NaN in multiplication.
  3. Step 3: Calculate 'total' as product of 'units' and 'price'

    Multiply filled columns to get correct totals.
  4. Final Answer:

    Fill NaNs first, then multiply -> Option C
  5. Quick Check:

    Fill NaN before math to avoid NaN results [OK]
Hint: Fill missing values with 0 before multiplying columns [OK]
Common Mistakes:
  • Multiplying columns with NaN without filling
  • Adding instead of multiplying for total
  • Filling NaN after multiplication
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard