📖 Scenario: Imagine you work at a small bookstore. You have two lists: one with book titles and their authors, and another with book titles and their prices. To understand your inventory better, you want to combine these lists to see each book's title, author, and price together.
🎯 Goal: Build a combined dataset that shows each book's title, author, and price by joining two separate datasets.
📋 What You'll Learn
Create a dictionary called books_authors with book titles as keys and authors as values.
Create a dictionary called books_prices with book titles as keys and prices as values.
Create a list of dictionaries called combined_books where each dictionary has keys title, author, and price.
Print the combined_books list to show the complete picture.
💡 Why This Matters
🌍 Real World
Combining datasets helps businesses see all related information in one place, like matching customer info with their orders.
💼 Career
Data analysts often combine data from different sources to create reports and insights that help companies make decisions.
Progress0 / 4 steps
1
Create the books and authors dataset
Create a dictionary called books_authors with these exact entries: 'The Hobbit': 'J.R.R. Tolkien', '1984': 'George Orwell', 'Pride and Prejudice': 'Jane Austen'.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary with book titles as keys and authors as values.
2
Create the books and prices dataset
Create a dictionary called books_prices with these exact entries: 'The Hobbit': 15.99, '1984': 12.50, 'Pride and Prejudice': 9.99.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary with book titles as keys and prices as values.
3
Combine the datasets into one list
Create a list called combined_books that contains dictionaries for each book. Each dictionary should have keys 'title', 'author', and 'price'. Use a for loop with variables title and author to iterate over books_authors.items() and get the price from books_prices.
Data Analysis Python
Hint
Use a for loop to go through books_authors. For each book, get the price from books_prices and add a dictionary to combined_books.
4
Print the combined dataset
Write a print statement to display the combined_books list.
Data Analysis Python
Hint
Use print(combined_books) to show the final combined list.
Practice
(1/5)
1. Why do we combine datasets in data analysis?
easy
A. To make data smaller by removing columns
B. To delete unnecessary data permanently
C. To get a more complete view by joining related information
D. To create random data for testing
Solution
Step 1: Understand the purpose of combining datasets
Combining datasets helps bring together related pieces of information from different sources.
Step 2: Recognize the benefit of combining
This creates a fuller, more complete picture for analysis.
Final Answer:
To get a more complete view by joining related information -> Option C
Quick Check:
Combining datasets = fuller picture [OK]
Hint: Combining means joining info to see the whole story [OK]
Common Mistakes:
Thinking combining deletes data
Confusing combining with filtering
Believing combining creates random data
2. Which of the following is the correct syntax to merge two pandas DataFrames df1 and df2 on a common column 'id'?
easy
A. pd.merge(df1, df2, on='id')
B. df1.merge(df2, by='id')
C. merge(df1, df2, key='id')
D. pd.join(df1, df2, on='id')
Solution
Step 1: Recall pandas merge syntax
The correct function is pd.merge() with parameters for the two DataFrames and the column name using on='id'.
Step 2: Check each option
pd.merge(df1, df2, on='id') uses correct function and parameter names. Others use incorrect method names or parameters.
You want a combined table showing all stores with their sales if available, and location if available. Which merge command gives this complete picture?
hard
A. pd.merge(sales, stores, on='store_id', how='inner')
B. pd.merge(sales, stores, on='store_id', how='left')
C. pd.merge(sales, stores, on='store_id', how='right')
D. pd.merge(sales, stores, on='store_id', how='outer')
Solution
Step 1: Understand join types and goal
You want all stores from both datasets, showing sales and location where available.
Step 2: Choose join type that keeps all rows
Outer join keeps all rows from both DataFrames, filling missing values with NaN.
Step 3: Verify options
Inner join keeps only matching stores. Left or right join keep only one side's stores. Outer join keeps all.
Final Answer:
pd.merge(sales, stores, on='store_id', how='outer') -> Option D
Quick Check:
Outer join = all data combined [OK]
Hint: Use outer join to keep all rows from both datasets [OK]
Common Mistakes:
Using inner join and losing unmatched rows
Confusing left/right join with full outer join
Expecting missing data to be dropped automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.