Merging on index means combining two data tables using their row labels (index) instead of columns. It matches rows based on their index values.
Click to reveal answer
beginner
How do you merge two DataFrames on their indexes in pandas?
Use the pandas merge function with the parameters left_index=True and right_index=True to merge on the indexes of both DataFrames.
Click to reveal answer
intermediate
What is the difference between merging on columns and merging on index?
Merging on columns uses matching values in specified columns to combine rows, while merging on index uses the row labels (index) to match rows.
Click to reveal answer
intermediate
What happens if indexes do not match when merging on index?
Rows with indexes that do not match will be included or excluded depending on the type of merge (inner, outer, left, right). Non-matching rows may have missing values.
Click to reveal answer
beginner
Write a simple pandas code snippet to merge two DataFrames df1 and df2 on their indexes.
Which parameters do you set to True to merge two DataFrames on their indexes?
Aleft_index=True, right_index=True
Bon='index'
Cindex=True
Dleft_on='index', right_on='index'
✗ Incorrect
To merge on indexes, set left_index=True and right_index=True in pandas merge.
What type of merge includes all rows from both DataFrames, matching where possible?
Ainner
Bright
Cleft
Douter
✗ Incorrect
An outer merge includes all rows from both DataFrames, filling missing matches with NaN.
If you want to merge on a column instead of index, which parameter do you use?
Aon='column_name'
Bright_index=True
Cleft_index=True
Dindex=True
✗ Incorrect
Use on='column_name' to merge on a specific column.
What will happen if indexes do not match in an inner merge on index?
AAll rows are kept
BOnly rows with matching indexes are kept
CRows with unmatched indexes get NaN values
DMerge will fail
✗ Incorrect
Inner merge keeps only rows with matching indexes.
Which pandas function is used to merge DataFrames?
Aconcat()
Bjoin()
Cmerge()
Dappend()
✗ Incorrect
The merge() function is used to combine DataFrames based on columns or indexes.
Explain how to merge two pandas DataFrames on their indexes and what parameters are needed.
Think about how row labels are used instead of columns.
You got /4 concepts.
Describe the difference between merging on columns and merging on index in pandas.
Consider what pandas uses to align rows in each case.
You got /4 concepts.
Practice
(1/5)
1. What does setting left_index=True and right_index=True do when merging two pandas DataFrames?
easy
A. It merges the DataFrames by ignoring the indexes completely.
B. It merges the DataFrames using their column names as the key.
C. It merges the DataFrames by concatenating columns without any key.
D. It merges the DataFrames using their row indexes as the key.
Solution
Step 1: Understand the role of indexes in merge
Indexes are row labels in pandas DataFrames. Using left_index=True and right_index=True tells pandas to join on these row labels instead of columns.
Step 2: Effect of these parameters in merge
When both are True, pandas matches rows based on their index values, combining rows with the same index from both DataFrames.
Final Answer:
It merges the DataFrames using their row indexes as the key. -> Option D
Quick Check:
Merging on indexes = left_index=True and right_index=True [OK]
Hint: Indexes join rows by labels, not columns [OK]
Common Mistakes:
Thinking merge always uses columns as keys
Confusing index merge with concatenation
Ignoring the need to set both left_index and right_index
2. Which of the following is the correct syntax to merge two DataFrames df1 and df2 on their indexes with an inner join?
easy
A. pd.merge(df1, df2, on='index', how='inner')
B. pd.merge(df1, df2, left_index=True, right_index=True, how='inner')
C. pd.merge(df1, df2, index=True, how='inner')
D. pd.merge(df1, df2, left_on='index', right_on='index', how='inner')
Solution
Step 1: Identify correct parameters for index merge
To merge on indexes, pandas requires left_index=True and right_index=True. Using on='index' or left_on='index' is incorrect because 'index' is not a column name.
Step 2: Confirm the join type
The how='inner' parameter specifies an inner join, which keeps only matching index rows.
Final Answer:
pd.merge(df1, df2, left_index=True, right_index=True, how='inner') -> Option B
Quick Check:
Use left_index and right_index for index merge [OK]
Hint: Use left_index and right_index, not 'on' for index merge [OK]
Common Mistakes:
Using on='index' instead of left_index=True
Confusing index merge with column merge
Forgetting to set both left_index and right_index
3. Given the following DataFrames:
df1:
A
idx
1 10
2 20
3 30
df2:
B
idx
2 200
3 300
4 400
What is the result of pd.merge(df1, df2, left_index=True, right_index=True, how='inner')?
medium
A. {'idx': [2, 3], 'A': [20, 30], 'B': [200, 300]}
C. left_index and right_index cannot be used together.
D. DataFrames must have the same index values for merge.
Solution
Step 1: Check parameters used in merge
The code uses left_index=True and right_index=True correctly to merge on indexes with how='outer'.
Step 2: Understand outer join behavior
Outer join includes all indexes from both DataFrames, filling missing values with NaN. This is valid and runs without error.
Final Answer:
No error; the code runs and merges correctly. -> Option A
Quick Check:
left_index and right_index merge works fine [OK]
Hint: Outer join merges all indexes, no error with index merge [OK]
Common Mistakes:
Assuming missing 'on' causes error when using index merge
Thinking left_index and right_index cannot be combined
Believing indexes must match exactly for merge
5. You have two DataFrames:
df1:
Score
Student
Alice 85
Bob 90
Charlie 78
df2:
Grade
Student
Bob B
Charlie C
David A
You want to merge them so that all students from df1 appear, and matching grades from df2 are added. Missing grades should show as NaN. Which code achieves this?
hard
A. pd.merge(df1, df2, left_index=True, right_index=True, how='right')
B. pd.merge(df1, df2, left_index=True, right_index=True, how='inner')
C. pd.merge(df1, df2, left_index=True, right_index=True, how='left')
D. pd.merge(df1, df2, left_on='Student', right_on='Student', how='outer')
Solution
Step 1: Identify merge type for all df1 rows
To keep all rows from df1 and add matching data from df2, use a left join.
Step 2: Merge on indexes
Both DataFrames use 'Student' as index, so set left_index=True and right_index=True to merge on indexes.
Final Answer:
pd.merge(df1, df2, left_index=True, right_index=True, how='left') -> Option C
Quick Check:
Left join on indexes keeps all df1 rows [OK]
Hint: Left join keeps all left DataFrame rows [OK]
Common Mistakes:
Using inner join which drops unmatched df1 rows
Using right join which keeps df2 rows instead
Merging on columns instead of indexes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.