The merge() function combines two DataFrames based on common columns or indices, similar to SQL joins.
Click to reveal answer
beginner
Name the four main types of joins you can perform with merge().
The four main join types are: inner, left, right, and outer joins.
Click to reveal answer
beginner
What is an inner join in merge()?
An inner join returns only rows with keys that appear in both DataFrames.
Click to reveal answer
intermediate
How do you specify the columns to join on in merge()?
Use the on parameter to specify the column name(s) to join on. You can also use left_on and right_on for different column names.
Click to reveal answer
beginner
What happens if you perform a merge() without specifying the how parameter?
By default, merge() performs an inner join, returning only matching rows from both DataFrames.
Click to reveal answer
Which how parameter value returns all rows from the left DataFrame and matching rows from the right?
Aright
Bleft
Cinner
Douter
✗ Incorrect
The 'left' join returns all rows from the left DataFrame and matching rows from the right.
If you want to join on columns with different names in each DataFrame, which parameters do you use?
Aon
Bhow
Cindex
Dleft_on and right_on
✗ Incorrect
Use 'left_on' and 'right_on' to specify different column names to join on.
What type of join does merge() perform by default?
Ainner
Bleft
Cright
Douter
✗ Incorrect
By default, 'merge()' performs an inner join.
Which join type returns all rows from both DataFrames, filling missing values with NaN?
Aouter
Bleft
Cright
Dinner
✗ Incorrect
An outer join returns all rows from both DataFrames, filling missing matches with NaN.
What parameter would you use to join DataFrames based on their index?
Aon
Bhow
Cleft_index and right_index
Dsort
✗ Incorrect
Use 'left_index=True' and 'right_index=True' to join on indices.
Explain how you would use merge() to combine two DataFrames with a common column named 'id'.
Think about the simplest join on a shared column.
You got /4 concepts.
Describe the difference between an inner join and an outer join using merge().
Consider what happens to rows without matches.
You got /3 concepts.
Practice
(1/5)
1. What does the how parameter in merge() control when joining two DataFrames?
easy
A. The data types of the columns
B. The columns to join on
C. The order of rows in the result
D. The type of join: inner, left, right, or outer
Solution
Step 1: Understand the role of how in merge()
The how parameter specifies the type of join to perform, such as inner, left, right, or outer join.
Step 2: Differentiate from other parameters
Other parameters like on specify columns to join on, but how controls how rows are matched and combined.
Final Answer:
The type of join: inner, left, right, or outer -> Option D
Quick Check:
how = join type [OK]
Hint: Remember: 'how' means join type like SQL joins [OK]
Common Mistakes:
Confusing 'how' with 'on' parameter
Thinking 'how' controls column selection
Assuming 'how' changes data types
2. Which of the following is the correct syntax to merge two DataFrames df1 and df2 on the column 'id' using an inner join?
easy
A. pd.merge(df1, df2, join='id', how='inner')
B. pd.merge(df1, df2, on='id', how='inner')
C. df1.merge(df2, on='id', type='inner')
D. pd.merge(df1, df2, on='id', join='inner')
Solution
Step 1: Identify correct parameter names
The correct parameters for merging are on for the column and how for join type.
Step 2: Check syntax correctness
pd.merge(df1, df2, on='id', how='inner') uses pd.merge(df1, df2, on='id', how='inner'), which is the correct syntax. Other options use incorrect parameter names like join or type.
Final Answer:
pd.merge(df1, df2, on='id', how='inner') -> Option B
Quick Check:
Correct parameters: on, how [OK]
Hint: Use 'on' for columns and 'how' for join type [OK]
Common Mistakes:
Using 'join' instead of 'how'
Using 'type' instead of 'how'
Mixing parameter names
3. Given the following code, what is the output of result?
A. DataFrames must have the same number of rows to merge
B. The 'how' parameter cannot be 'left'
C. The merge function is missing the 'left_on' and 'right_on' parameters
D. Column 'id' does not exist in both DataFrames
Solution
Step 1: Check columns used for merging
The code tries to merge on column 'id', but df2 has 'key' column instead of 'id'.
Step 2: Understand merge requirements
For merging on different column names, left_on and right_on must be used, or columns must have the same name.
Final Answer:
The merge function is missing the 'left_on' and 'right_on' parameters -> Option C
Quick Check:
Merge columns must exist in both DataFrames or be specified [OK]
Hint: Check column names exist in both DataFrames or specify left_on/right_on [OK]
Common Mistakes:
Assuming merge works with different column names without specifying
Thinking 'how' parameter is invalid
Believing row counts must match
5. You have two DataFrames: orders with columns order_id, customer_id, and amount, and customers with columns cust_id and name. How do you merge them to get all orders with customer names, even if some customers are missing in customers DataFrame?
hard
A. pd.merge(orders, customers, left_on='customer_id', right_on='cust_id', how='left')
B. pd.merge(orders, customers, on='customer_id', how='inner')
C. pd.merge(orders, customers, left_on='cust_id', right_on='customer_id', how='right')
D. pd.merge(orders, customers, on='cust_id', how='outer')
Solution
Step 1: Identify join keys with different column names
Orders use 'customer_id', customers use 'cust_id', so use left_on and right_on to specify.
Step 2: Choose join type to keep all orders
Use how='left' to keep all rows from orders, even if no matching customer exists.
Final Answer:
pd.merge(orders, customers, left_on='customer_id', right_on='cust_id', how='left') -> Option A
Quick Check:
Left join keeps all left rows [OK]
Hint: Use left_on/right_on for different keys and 'left' to keep all left rows [OK]
Common Mistakes:
Using 'on' with different column names
Using inner join and losing unmatched orders
Swapping left_on and right_on
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.