📖 Scenario: You work in a small bookstore. You have two lists of information: one list has book titles and their authors, and another list has book titles and their prices. You want to combine these lists to see all books with their authors and prices, but some books might not have prices yet.
🎯 Goal: Build a pandas DataFrame that shows all books with their authors and prices using a left join. This means every book from the first list appears, and prices are added when available.
📋 What You'll Learn
Create a pandas DataFrame called books with columns title and author using the exact data provided.
Create a pandas DataFrame called prices with columns title and price using the exact data provided.
Create a variable called merged_books that contains the result of a left join of books with prices on the title column.
Use pandas merge function with how='left' to perform the join.
💡 Why This Matters
🌍 Real World
Combining data from different sources is common in business, like merging customer info with purchase records.
💼 Career
Data analysts and scientists often join tables to prepare data for reports and insights.
Progress0 / 4 steps
1
Create the books DataFrame
Create a pandas DataFrame called books with these exact entries: titles 'The Hobbit', '1984', 'Python 101', and authors 'J.R.R. Tolkien', 'George Orwell', 'Michael Driscoll'. Use columns named title and author.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Create the prices DataFrame
Create a pandas DataFrame called prices with these exact entries: titles 'The Hobbit', 'Python 101', and prices 10.99, 29.99. Use columns named title and price.
Pandas
Hint
Use pd.DataFrame with a dictionary for titles and prices.
3
Perform the left join
Create a variable called merged_books that contains the result of left joining books with prices on the title column using pandas merge function with how='left'.
Pandas
Hint
Use pd.merge() with on='title' and how='left' to keep all books and add prices where available.
4
Complete the DataFrame setup
Add a line to set the index of merged_books to the title column using the set_index method with inplace=True.
Pandas
Hint
Use set_index with inplace=True to change the DataFrame index without creating a new one.
Practice
(1/5)
1. What does a left join do in pandas when merging two DataFrames?
easy
A. Removes all rows that do not have matches in either DataFrame.
B. Keeps all rows from the left DataFrame and adds matching rows from the right DataFrame.
C. Keeps all rows from the right DataFrame and adds matching rows from the left DataFrame.
D. Keeps only rows that match in both DataFrames.
Solution
Step 1: Understand left join behavior
A left join keeps all rows from the left DataFrame regardless of matches.
Step 2: Check how matching rows are added
Matching rows from the right DataFrame are added; unmatched right rows are ignored.
Final Answer:
Keeps all rows from the left DataFrame and adds matching rows from the right DataFrame. -> Option B
Quick Check:
Left join = all left rows + matching right rows [OK]
Hint: Left join keeps all left rows, adds matching right rows [OK]
Common Mistakes:
Confusing left join with inner join
Thinking unmatched right rows appear in output
Mixing up left and right join behavior
2. Which of the following is the correct syntax to perform a left join of DataFrames df1 and df2 on column 'id' in pandas?
easy
A. pd.merge(df1, df2, on='id', how='left')
B. pd.merge(df1, df2, on='id', how='inner')
C. df1.join(df2, on='id', how='right')
D. pd.concat([df1, df2], join='left')
Solution
Step 1: Identify correct merge function and parameters
Use pd.merge with 'on' for join column and 'how="left"' for left join.
Step 2: Check each option's correctness
pd.merge(df1, df2, on='id', how='left') uses correct function and parameters; others use wrong join type or method.
Final Answer:
pd.merge(df1, df2, on='id', how='left') -> Option A
Quick Check:
Left join syntax = pd.merge(..., how='left') [OK]
Hint: Use pd.merge with how='left' for left join [OK]