It combines two DataFrames based on common columns or indices, similar to SQL joins.
Click to reveal answer
beginner
What are the main types of joins you can perform with merge()?
Inner join: keeps only matching rows
Left join: keeps all rows from left DataFrame
Right join: keeps all rows from right DataFrame
Outer join: keeps all rows from both DataFrames
Click to reveal answer
beginner
How do you specify the columns to join on in merge()?
Use the on parameter with a column name or list of column names that exist in both DataFrames.
Click to reveal answer
beginner
What happens if you use how='left' in merge()?
All rows from the left DataFrame are kept. Matching rows from the right DataFrame are added. If no match, right side columns have NaN.
Click to reveal answer
intermediate
What is the difference between merge() and concat() in pandas?
merge() joins DataFrames based on keys (like SQL joins). concat() stacks DataFrames vertically or horizontally without matching keys.
Click to reveal answer
Which how parameter in merge() keeps only rows that appear in both DataFrames?
Aright
Bleft
Cinner
Douter
✗ Incorrect
The 'inner' join keeps only rows with matching keys in both DataFrames.
If you want to keep all rows from the right DataFrame and add matching rows from the left, which how do you use?
Ainner
Bright
Cleft
Douter
✗ Incorrect
The 'right' join keeps all rows from the right DataFrame and matches from the left.
What parameter do you use to specify the column(s) to join on in merge()?
Akey
Bby
Cjoin
Don
✗ Incorrect
The 'on' parameter specifies the column(s) to join on.
What will be the result of an outer join?
AAll rows from both DataFrames, filling missing with NaN
BAll rows from the left DataFrame only
COnly rows common to both DataFrames
DOnly rows from the right DataFrame
✗ Incorrect
An outer join keeps all rows from both DataFrames, filling missing matches with NaN.
Which pandas function stacks DataFrames without matching keys?
Aconcat()
Bmerge()
Cjoin()
Dgroupby()
✗ Incorrect
concat() stacks DataFrames vertically or horizontally without matching keys.
Explain how to use pandas merge() to combine two DataFrames with a left join.
Think about keeping all rows from the first DataFrame and matching from the second.
You got /4 concepts.
Describe the difference between inner join and outer join in pandas merge().
Consider which rows are kept in each join type.
You got /3 concepts.
Practice
(1/5)
1. What does the merge() function in pandas do?
easy
A. It filters rows based on a condition.
B. It sorts a DataFrame by column values.
C. It changes the data type of a column.
D. It combines two DataFrames based on matching columns.
Solution
Step 1: Understand the purpose of merge()
The merge() function is designed to combine two tables (DataFrames) by matching values in specified columns.
Step 2: Compare with other options
Sorting, filtering, and changing data types are different operations and not the main purpose of merge().
Final Answer:
It combines two DataFrames based on matching columns. -> Option D
Quick Check:
merge() joins tables on columns [OK]
Hint: merge() joins tables by matching columns [OK]
Common Mistakes:
Confusing merge() with sort or filter functions
Thinking merge() changes data types
Assuming merge() works without matching columns
2. Which of the following is the correct syntax to perform an inner join of two DataFrames df1 and df2 on column 'id' using pandas?
easy
A. df1.merge(df2, by='id', join='inner')
B. pd.merge(df1, df2, on='id', how='inner')
C. pd.join(df1, df2, on='id', how='inner')
D. df1.merge(df2, on='id', how='outer')
Solution
Step 1: Recall correct merge syntax
The correct pandas syntax for merging is pd.merge(left, right, on=column, how=type). Here, how='inner' specifies an inner join.
Step 2: Check each option
pd.merge(df1, df2, on='id', how='inner') matches the correct syntax. df1.merge(df2, by='id', join='inner') uses wrong parameter names. pd.join(df1, df2, on='id', how='inner') uses pd.join which does not exist. df1.merge(df2, on='id', how='outer') uses how='outer' which is not inner join.
Final Answer:
pd.merge(df1, df2, on='id', how='inner') -> Option B
You want to create a DataFrame showing all customers and their orders, including customers with no orders and orders with no matching customer. Which merge() call achieves this?
hard
A. pd.merge(df_customers, df_orders, on='customer_id', how='left')
B. pd.merge(df_customers, df_orders, on='customer_id', how='inner')
C. pd.merge(df_customers, df_orders, on='customer_id', how='outer')
D. pd.merge(df_customers, df_orders, on='customer_id', how='right')
Solution
Step 1: Understand join types
An outer join keeps all rows from both DataFrames, filling missing matches with NaN.
Step 2: Apply to problem context
We want all customers (even without orders) and all orders (even without matching customers). Only outer join includes all rows from both sides.
Final Answer:
pd.merge(df_customers, df_orders, on='customer_id', how='outer') -> Option C
Quick Check:
Outer join includes all rows from both tables [OK]
Hint: Use how='outer' to keep all rows from both DataFrames [OK]
Common Mistakes:
Using inner join excludes unmatched rows
Left join excludes orders without customers
Right join excludes customers without orders
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.