📖 Scenario: You work in a small company that keeps two separate lists: one for employees and one for their departments. You want to combine these lists to see which employees belong to which departments.
🎯 Goal: Build a pandas DataFrame that shows employees matched with their departments using an inner join.
📋 What You'll Learn
Create two pandas DataFrames named employees and departments with exact data.
Create a variable key_column with the column name to join on.
Use pandas merge function with how='inner' to join the DataFrames on key_column.
Store the result in a DataFrame named employee_department.
💡 Why This Matters
🌍 Real World
Companies often keep separate lists for employees and departments. Joining these lists helps to see which employee works in which department.
💼 Career
Understanding inner joins is essential for data analysts and database professionals to combine related data from multiple tables or sources.
Progress0 / 4 steps
1
Create the employees DataFrame
Create a pandas DataFrame called employees with these exact rows and columns: EmployeeID and Name. The rows are: 1, 'Alice', 2, 'Bob', 3, 'Charlie', 4, 'David'.
Pandas
Hint
Use pd.DataFrame with a dictionary where keys are column names and values are lists of column values.
2
Create the departments DataFrame and key column
Create a pandas DataFrame called departments with columns EmployeeID and Department. The rows are: 1, 'HR', 2, 'Engineering', 4, 'Marketing', 5, 'Sales'. Then create a variable called key_column and set it to the string 'EmployeeID'.
Pandas
Hint
Use the same method as Step 1 to create departments. Then assign the string 'EmployeeID' to key_column.
3
Perform the inner join
Use the pandas merge function to join employees and departments on the column stored in key_column. Use how='inner' to keep only matching rows. Store the result in a DataFrame called employee_department.
Pandas
Hint
Use pd.merge(employees, departments, on=key_column, how='inner') to join.
4
Complete the DataFrame with correct columns
Ensure the employee_department DataFrame contains exactly these columns in this order: EmployeeID, Name, and Department. If needed, reorder the columns explicitly.
Pandas
Hint
Use bracket notation to reorder columns: employee_department = employee_department[['EmployeeID', 'Name', 'Department']].
Practice
(1/5)
1. What does an inner join do when merging two pandas DataFrames?
easy
A. Keeps only rows with matching keys in both DataFrames
B. Keeps all rows from the left DataFrame and fills missing with NaN
C. Keeps all rows from the right DataFrame and fills missing with NaN
D. Keeps all rows from both DataFrames, filling missing with NaN
Solution
Step 1: Understand join types in pandas
Inner join keeps only rows where the key exists in both DataFrames.
Step 2: Compare with other join types
Left join keeps all left rows, right join keeps all right rows, outer join keeps all rows from both.
Final Answer:
Keeps only rows with matching keys in both DataFrames -> Option A
Quick Check:
Inner join = matching keys only [OK]
Hint: Inner join keeps only common keys in both tables [OK]
Common Mistakes:
Confusing inner join with left or outer join
Thinking inner join keeps all rows
Assuming inner join fills missing with NaN
2. Which of the following is the correct syntax to perform an inner join on DataFrames df1 and df2 using the column 'id'?
easy
A. pd.merge(df1, df2, on='id', how='inner')
B. pd.merge(df1, df2, on='id', how='outer')
C. df1.join(df2, on='id')
D. df1.merge(df2, left_on='id', right_on='id', how='left')
Solution
Step 1: Identify correct function and parameters
Use pd.merge() with on='id' and how='inner' for inner join.
Step 2: Check other options for errors
df1.join(df2, on='id') defaults to left join, pd.merge(df1, df2, on='id', how='outer') uses outer join, df1.merge(df2, left_on='id', right_on='id', how='left') uses left join.
Final Answer:
pd.merge(df1, df2, on='id', how='inner') -> Option A