📖 Scenario: You work in a small company that keeps two separate lists: one for employees and one for their assigned projects. Sometimes, employees may not have projects yet, and sometimes projects may not have assigned employees. You want to combine these lists to see all employees and projects together, even if some don't match.
🎯 Goal: Build a pandas DataFrame that shows all employees and all projects combined using an outer join. This means you will see every employee and every project, matched where possible, and with missing data filled with NaN.
📋 What You'll Learn
Create two pandas DataFrames: employees and projects with exact data.
Create a variable join_key with the column name to join on.
Use pandas merge function with how='outer' to join the DataFrames on join_key.
Store the result in a variable called combined.
💡 Why This Matters
🌍 Real World
Combining employee and project data from separate sources to get a complete view of assignments.
💼 Career
Data analysts and database professionals often need to join tables to create comprehensive reports.
Progress0 / 4 steps
1
Create the employees DataFrame
Create a pandas DataFrame called employees with these exact columns and data: 'emp_id' with values [1, 2, 3] and 'emp_name' with values ['Alice', 'Bob', 'Charlie'].
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of data.
2
Create the projects DataFrame
Create a pandas DataFrame called projects with these exact columns and data: 'emp_id' with values [2, 3, 4] and 'project_name' with values ['Project X', 'Project Y', 'Project Z'].
Pandas
Hint
Use pd.DataFrame with a dictionary for the projects data.
3
Set the join key
Create a variable called join_key and set it to the string 'emp_id'. This will be the column used to join the DataFrames.
Pandas
Hint
Just assign the string 'emp_id' to the variable join_key.
4
Perform the outer join
Use the pandas merge function to join employees and projects on the column join_key with how='outer'. Store the result in a variable called combined.
Pandas
Hint
Use pd.merge(employees, projects, on=join_key, how='outer') and assign it to combined.
Practice
(1/5)
1. What does an outer join do when merging two DataFrames in pandas?
easy
A. Keeps only rows from the right DataFrame
B. Keeps only rows that match in both DataFrames
C. Keeps all rows from both DataFrames, filling missing values with NaN
D. Keeps only rows from the left DataFrame
Solution
Step 1: Understand the join types in pandas
Inner join keeps only matching rows, left join keeps all from left, right join keeps all from right, outer join keeps all from both.
Step 2: Identify behavior of outer join
Outer join includes all rows from both DataFrames and fills missing matches with NaN.
Final Answer:
Keeps all rows from both DataFrames, filling missing values with NaN -> Option C
Quick Check:
Outer join = all rows kept with NaN for missing [OK]
Hint: Outer join keeps all rows, fills missing with NaN [OK]
Common Mistakes:
Confusing outer join with inner join
Thinking outer join keeps only left or right rows
Assuming missing data is dropped instead of NaN
2. Which of the following is the correct syntax to perform an outer join on DataFrames df1 and df2 using pandas?
easy
A. pd.merge(df1, df2, how='outer')
B. pd.merge(df1, df2, join='outer')
C. df1.join(df2, join='outer')
D. df1.merge(df2, type='outer')
Solution
Step 1: Recall the pandas merge syntax
The function pd.merge() takes parameters including left DataFrame, right DataFrame, and how to join.
Step 2: Identify correct parameter for outer join
The parameter to specify join type is how='outer'. Other options like join='outer' or type='outer' are invalid.
Final Answer:
pd.merge(df1, df2, how='outer') -> Option A
Quick Check:
Use how='outer' in pd.merge() [OK]
Hint: Use how='outer' in pd.merge() for outer join [OK]
Common Mistakes:
Using join='outer' instead of how='outer'
Using type='outer' which is invalid
Using df1.join() without specifying correct parameters