0
0
Pandasdata~10 mins

Merging on different column names in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to merge two DataFrames on columns with different names.

Pandas
merged_df = df1.merge(df2, left_on=[1], right_on='id')
Drag options to blanks, or click blank then click option'
A'key'
B'id'
C'value'
D'index'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' for left_on which is the column name in the second DataFrame.
Using 'index' which is not a column name here.
2fill in blank
medium

Complete the code to merge on columns with different names using suffixes to distinguish overlapping columns.

Pandas
merged_df = df1.merge(df2, left_on='key', right_on=[1], suffixes=('_left', '_right'))
Drag options to blanks, or click blank then click option'
A'key'
B'value'
C'id'
D'index'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'key' for right_on which is the column name in the first DataFrame.
Using 'value' which is unrelated.
3fill in blank
hard

Fix the error in the merge code by choosing the correct parameter for merging on different column names.

Pandas
merged_df = df1.merge(df2, on=[1])
Drag options to blanks, or click blank then click option'
ANone
B'id'
C'key'
D['key', 'id']
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to merge on a list of different column names with 'on' parameter.
Using a single column name that exists only in one DataFrame.
4fill in blank
hard

Fill both blanks to create a merge that joins df1 and df2 on different column names and keeps all rows from df1.

Pandas
merged_df = df1.merge(df2, left_on=[1], right_on=[2], how='left')
Drag options to blanks, or click blank then click option'
A'key'
B'value'
C'id'
D'index'
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the left_on and right_on values.
Using 'value' or 'index' which are not join columns.
5fill in blank
hard

Fill all three blanks to merge df1 and df2 on different column names, keep all rows from df2, and add suffixes to overlapping columns.

Pandas
merged_df = df1.merge(df2, left_on=[1], right_on=[2], how=[3], suffixes=('_df1', '_df2'))
Drag options to blanks, or click blank then click option'
A'key'
B'id'
C'right'
D'outer'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'left' or 'outer' instead of 'right' for the how parameter.
Mixing up left_on and right_on values.