0
0
Data Analysis Pythondata~10 mins

Merging on multiple keys in Data Analysis Python - 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 'key1' and 'key2'.

Data Analysis Python
merged_df = df1.merge(df2, on=[1])
Drag options to blanks, or click blank then click option'
A['key1', 'key2']
B['key1']
C'key1'
D'key2'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing a single string instead of a list for multiple keys.
Using only one key when two are needed.
2fill in blank
medium

Complete the code to perform a left merge on columns 'city' and 'year'.

Data Analysis Python
result = df_left.merge(df_right, on=[1], how='left')
Drag options to blanks, or click blank then click option'
A['city']
B'city'
C'year'
D['city', 'year']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single column name instead of a list.
Forgetting to specify the how parameter for merge type.
3fill in blank
hard

Fix the error in merging on multiple keys by completing the code.

Data Analysis Python
merged = pd.merge(df1, df2, on=[1])
Drag options to blanks, or click blank then click option'
A'key1,key2'
B['key1', 'key2']
C'key1'
D'key2'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single string with comma-separated keys.
Passing only one key when two are needed.
4fill in blank
hard

Fill both blanks to create a merged DataFrame on 'state' and 'month' with an inner join.

Data Analysis Python
merged_df = df1.merge(df2, on=[1], how=[2])
Drag options to blanks, or click blank then click option'
A['state', 'month']
B'outer'
C'inner'
D['month', 'state']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a list for on.
Using the wrong join type in how.
5fill in blank
hard

Fill all three blanks to merge on 'country' and 'year' with a right join and suffixes '_left' and '_right'.

Data Analysis Python
merged = df1.merge(df2, on=[1], how=[2], suffixes=([3], '_right'))
Drag options to blanks, or click blank then click option'
A['country', 'year']
B'right'
C'_left'
D'inner'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing suffixes as strings instead of a tuple.
Using incorrect join type in how.
Using a string instead of a list for on.