0
0
Data Analysis Pythondata~20 mins

Left and right joins in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Join Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
data_output
intermediate
2: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)
A
  key  value1  value2
0   A       1     NaN
1   B       2     4.0
2   C       3     5.0
B
  key  value1  value2
0   B       2     4.0
1   C       3     5.0
2   D     NaN     6.0
C
  key  value1  value2
0   A       1     4.0
1   B       2     5.0
2   C       3     6.0
D
  key  value1  value2
0   A       1     NaN
1   B       2     NaN
2   C       3     NaN
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.
data_output
intermediate
2: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)
A
  key  value1  value2
0   X     7.0      NaN
1   Y     8.0      10
2   Z     9.0      11
B
  key  value1  value2
0   X     7.0      10
1   Y     8.0      11
2   Z     9.0      12
C
  key  value1  value2
0   Y     NaN      10
1   Z     NaN      11
2   W     NaN      12
D
  key  value1  value2
0   Y     8.0      10
1   Z     9.0      11
2   W     NaN      12
Attempts:
2 left
💡 Hint
Right join keeps all rows from the right DataFrame and matches from the left.
🧠 Conceptual
advanced
1:30remaining
Understanding join types in pandas
Which statement correctly describes the difference between a left join and a right join in pandas?
ALeft join keeps only matching rows; right join keeps all rows from both DataFrames.
BLeft join keeps all rows from the left DataFrame; right join keeps all rows from the right DataFrame.
CLeft join keeps all rows from the right DataFrame; right join keeps all rows from the left DataFrame.
DLeft join and right join both keep only matching rows from both DataFrames.
Attempts:
2 left
💡 Hint
Think about which DataFrame's rows are always kept in each join type.
🔧 Debug
advanced
1: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)
AKeyError: 'keys'
BTypeError: merge() got an unexpected keyword argument 'how'
CValueError: columns overlap but no suffix specified
DNo error; outputs merged DataFrame
Attempts:
2 left
💡 Hint
Check the column name used for joining.
🚀 Application
expert
2: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]})
APerform a full outer join and drop rows with NaN values.
BPerform a right join and filter rows where the left DataFrame columns are NaN.
CPerform a left join and filter rows where the right DataFrame columns are NaN.
DPerform an inner join and select rows with NaN values.
Attempts:
2 left
💡 Hint
Think about which join keeps all rows from df1 and how to identify unmatched rows.