Challenge - 5 Problems
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ data_output
intermediate2:00remaining
Output of a left join on two DataFrames
Given two DataFrames
df1 and df2, what is the output of the left join on column key?Data Analysis Python
import pandas as pd df1 = pd.DataFrame({'key': ['A', 'B', 'C'], 'value1': [1, 2, 3]}) df2 = pd.DataFrame({'key': ['B', 'C', 'D'], 'value2': [4, 5, 6]}) result = pd.merge(df1, df2, how='left', on='key') print(result)
Attempts:
2 left
💡 Hint
Remember, a left join keeps all rows from the left DataFrame and matches rows from the right DataFrame where keys match.
✗ Incorrect
A left join keeps all rows from the left DataFrame (df1). For keys that don't exist in the right DataFrame (df2), the joined columns get NaN. Here, 'A' is only in df1, so value2 is NaN for that row.
❓ data_output
intermediate2:00remaining
Output of a right join on two DataFrames
What is the output of the right join on column
key for the given DataFrames?Data Analysis Python
import pandas as pd df1 = pd.DataFrame({'key': ['X', 'Y', 'Z'], 'value1': [7, 8, 9]}) df2 = pd.DataFrame({'key': ['Y', 'Z', 'W'], 'value2': [10, 11, 12]}) result = pd.merge(df1, df2, how='right', on='key') print(result)
Attempts:
2 left
💡 Hint
Right join keeps all rows from the right DataFrame and matches from the left.
✗ Incorrect
Right join keeps all keys from df2. 'W' is only in df2, so value1 is NaN for that row. 'Y' and 'Z' exist in both, so values are matched.
🧠 Conceptual
advanced1:30remaining
Understanding join types in pandas
Which statement correctly describes the difference between a left join and a right join in pandas?
Attempts:
2 left
💡 Hint
Think about which DataFrame's rows are always kept in each join type.
✗ Incorrect
Left join keeps all rows from the left DataFrame and matches rows from the right. Right join keeps all rows from the right DataFrame and matches rows from the left.
🔧 Debug
advanced1:30remaining
Identify the error in join code
What error will this code produce?
Data Analysis Python
import pandas as pd df1 = pd.DataFrame({'key': ['A', 'B'], 'val1': [1, 2]}) df2 = pd.DataFrame({'key': ['B', 'C'], 'val2': [3, 4]}) result = pd.merge(df1, df2, how='left', on='keys') print(result)
Attempts:
2 left
💡 Hint
Check the column name used for joining.
✗ Incorrect
The code tries to join on column 'keys' which does not exist in either DataFrame. This causes a KeyError.
🚀 Application
expert2:30remaining
Combine two DataFrames with left and right joins to find unmatched rows
You have two DataFrames
df1 and df2. How can you find rows in df1 that do not have matching keys in df2 using joins?Data Analysis Python
import pandas as pd df1 = pd.DataFrame({'key': ['A', 'B', 'C', 'D'], 'val1': [1, 2, 3, 4]}) df2 = pd.DataFrame({'key': ['B', 'C'], 'val2': [5, 6]})
Attempts:
2 left
💡 Hint
Think about which join keeps all rows from df1 and how to identify unmatched rows.
✗ Incorrect
A left join keeps all rows from df1. Rows with no match in df2 will have NaN in df2 columns. Filtering these rows finds unmatched keys.