📖 Scenario: You work in a small store that keeps two lists: one for products and one for sales. You want to combine these lists to see which products sold and which did not.
🎯 Goal: Learn how to use left join and right join to combine two tables (dataframes) in Python using pandas. You will create two dataframes, set a join key, and then join them to see all products with their sales and all sales with their product details.
📋 What You'll Learn
Create two pandas DataFrames with exact data
Create a variable for the join key column name
Use pandas merge with how='left' and how='right' to join dataframes
Print the joined DataFrames to see the results
💡 Why This Matters
🌍 Real World
Stores and businesses often have separate lists for products and sales. Joining these lists helps understand which products sold and which did not.
💼 Career
Data analysts and scientists use joins to combine data from different sources to create meaningful reports and insights.
Progress0 / 4 steps
1
Create product and sales DataFrames
Import pandas as pd. Create a DataFrame called products with columns 'product_id' and 'product_name' containing these rows: (1, 'Apple'), (2, 'Banana'), (3, 'Carrot'). Create another DataFrame called sales with columns 'product_id' and 'quantity' containing these rows: (1, 10), (2, 5), (4, 7).
Data Analysis Python
Hint
Use pd.DataFrame with a dictionary of lists to create each DataFrame.
2
Set the join key variable
Create a variable called join_key and set it to the string 'product_id'. This will be the column used to join the two DataFrames.
Data Analysis Python
Hint
Just assign the string 'product_id' to the variable join_key.
3
Perform left and right joins
Use pd.merge to create a DataFrame called left_joined by joining products and sales on join_key with how='left'. Then create another DataFrame called right_joined by joining products and sales on join_key with how='right'.
Data Analysis Python
Hint
Use pd.merge() with the on parameter set to join_key and how set to 'left' or 'right'.
4
Print the joined DataFrames
Print the left_joined DataFrame. Then print the right_joined DataFrame.
Data Analysis Python
Hint
Use print() to show both DataFrames.
Practice
(1/5)
1. What does a left join do when combining two tables in data analysis?
easy
A. Keeps all rows from both tables, matching or not.
B. Keeps all rows from the left table and adds matching rows from the right table.
C. Keeps only rows that match in both tables.
D. Keeps all rows from the right table and adds matching rows from the left table.
Solution
Step 1: Understand the role of the left table in a left join
A left join keeps every row from the left table regardless of matches in the right table.
Step 2: Understand how matching rows from the right table are added
Only rows from the right table that match the left table's keys are added; unmatched right rows are ignored.
Final Answer:
Keeps all rows from the left table and adds matching rows from the right table. -> Option B
Quick Check:
Left join = all left rows + matching right rows [OK]
Hint: Left join keeps all left rows, adds right matches [OK]
Common Mistakes:
Confusing left join with right join
Thinking left join keeps unmatched right rows
Believing left join only keeps matching rows
2. Which of the following is the correct syntax for a left join using pandas in Python?
easy
A. pd.merge(df1, df2, how='left', on='key')
B. pd.merge(df1, df2, how='right', on='key')
C. pd.join(df1, df2, how='left')
D. df1.left_join(df2, on='key')
Solution
Step 1: Recall pandas merge syntax for joins
In pandas, the function to join tables is pd.merge(), specifying the join type with the 'how' parameter.
Step 2: Identify the correct parameters for a left join
Using how='left' and on='key' correctly performs a left join on the specified key column.
Final Answer:
pd.merge(df1, df2, how='left', on='key') -> Option A
Quick Check:
pd.merge + how='left' = left join [OK]
Hint: Use pd.merge with how='left' for left join [OK]