📖 Scenario: You work in a small bookstore. You have a list of books with their details stored as dictionaries. You want to organize this data into a table to analyze it easily.
🎯 Goal: Create a pandas DataFrame from a list of dictionaries representing books. Then display the DataFrame.
📋 What You'll Learn
Create a list of dictionaries called books with exact entries
Create a pandas DataFrame called df from the books list
Print the DataFrame df to show the table
💡 Why This Matters
🌍 Real World
Organizing data from lists of dictionaries into tables is common in data analysis, such as managing inventory or customer data.
💼 Career
Data scientists and analysts often convert raw data into DataFrames to clean, analyze, and visualize information efficiently.
Progress0 / 4 steps
1
Create the list of dictionaries
Create a list called books with these exact dictionaries: {'title': 'The Alchemist', 'author': 'Paulo Coelho', 'year': 1988}, {'title': '1984', 'author': 'George Orwell', 'year': 1949}, {'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960}
Pandas
Hint
Use square brackets [] to create a list. Each book is a dictionary inside the list.
2
Import pandas and create DataFrame
Import the pandas library as pd. Then create a DataFrame called df from the list books using pd.DataFrame(books).
Pandas
Hint
Use import pandas as pd to import pandas. Then use pd.DataFrame() to create the table.
3
Print the DataFrame
Use print(df) to display the DataFrame df.
Pandas
Hint
Use print(df) to see the table of books.
4
Add a filter for books after 1950
Create a new DataFrame called recent_books that contains only the rows from df where the year is greater than 1950. Use df[df['year'] > 1950].
Pandas
Hint
Use df[df['year'] > 1950] to filter rows where year is greater than 1950.
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
Step 1: Understand DataFrame creation from list of dicts
Each dictionary in the list is treated as one row in the DataFrame.
Step 2: Identify how keys are used
Keys from dictionaries become the column names in the DataFrame.
Final Answer:
Each dictionary becomes a row, and keys become column names. -> Option A
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
Step 1: Recall pandas DataFrame constructor
The correct way to create a DataFrame from data is using pd.DataFrame().
Step 2: Check method names
There is no from_list, create, or list method in pandas DataFrame.
Final Answer:
df = pd.DataFrame(data) -> Option A
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
Step 1: Check keys across dictionaries
Keys are 'name', 'age' in first dict and 'name', 'city' in second dict.
Step 2: Understand pandas behavior with missing keys
Pandas creates columns for all keys found and fills missing values with NaN.
Final Answer:
DataFrame with columns 'name', 'age', 'city' and NaN where data is missing. -> Option D
Quick Check:
Missing keys -> NaN in DataFrame [OK]
Hint: Missing keys become NaN in DataFrame columns [OK]