📖 Scenario: You are working with two small datasets representing customers and their orders in a simple store system. You want to combine these datasets to see which customers placed which orders.
🎯 Goal: Build a Python script that performs an inner join on two dictionaries based on a common key, customer_id, to combine customer names with their order details.
📋 What You'll Learn
Create two dictionaries: customers and orders with exact entries
Create a list variable joined_data to store the inner join results
Use a for loop to iterate over orders and match customer_id with customers
Append tuples of (customer_name, order_id, product) to joined_data
💡 Why This Matters
🌍 Real World
Inner joins are used in databases and data analysis to combine related data from different tables based on common keys, like customer IDs.
💼 Career
Understanding inner joins is essential for data analysts, database administrators, and software developers working with relational data.
Progress0 / 4 steps
1
Create the customers dictionary
Create a dictionary called customers with these exact entries: 1: 'Alice', 2: 'Bob', 3: 'Charlie'.
Data Analysis Python
Hint
Use curly braces {} to create a dictionary with integer keys and string values.
2
Create the orders dictionary
Create a dictionary called orders with these exact entries: 101: {'customer_id': 1, 'product': 'Book'}, 102: {'customer_id': 2, 'product': 'Pen'}, 103: {'customer_id': 4, 'product': 'Notebook'}.
Data Analysis Python
Hint
Use nested dictionaries for each order with keys customer_id and product.
3
Create the joined_data list and loop to join
Create an empty list called joined_data. Use a for loop with variables order_id and order_info to iterate over orders.items(). Inside the loop, check if order_info['customer_id'] is in customers. If yes, append a tuple (customers[order_info['customer_id']], order_id, order_info['product']) to joined_data.
Data Analysis Python
Hint
Use orders.items() to get both order ID and info, then check if the customer ID exists in customers.
4
Complete the inner join by verifying joined_data
Add a final line that assigns result to joined_data to complete the inner join data structure.
Data Analysis Python
Hint
Simply assign the joined_data list to a new variable result to finalize.
Practice
(1/5)
1. What does an inner join do when combining two tables?
easy
A. Returns only rows with matching keys in both tables
B. Returns all rows from the first table, matching or not
C. Returns all rows from the second table, matching or not
D. Returns all rows from both tables, matching or not
Solution
Step 1: Understand join types
Inner join returns rows where keys match in both tables only.
Step 2: Compare options with definition
The option 'Returns only rows with matching keys in both tables' describes this behavior correctly.
Final Answer:
Returns only rows with matching keys in both tables -> Option A
Quick Check:
Inner join = matching rows only [OK]
Hint: Inner join keeps only matching rows from both tables [OK]
Common Mistakes:
Confusing inner join with left or right join
Thinking inner join returns unmatched rows
Mixing up join types and their results
2. Which of the following is the correct syntax to perform an inner join of two pandas DataFrames df1 and df2 on column 'id'?
easy
A. df1.join(df2, on='id', how='outer')
B. pd.concat([df1, df2], axis=1, join='inner')
C. pd.merge(df1, df2, on='id', how='inner')
D. df1.merge(df2, on='id', how='left')
Solution
Step 1: Identify pandas inner join syntax
The correct function is pd.merge() with how='inner' and on='id'.
Step 2: Check each option
pd.merge(df1, df2, on='id', how='inner') matches this exactly; others use wrong methods or join types.
Final Answer:
pd.merge(df1, df2, on='id', how='inner') -> Option C
Quick Check:
pd.merge + how='inner' = inner join [OK]
Hint: Use pd.merge with how='inner' for inner joins [OK]