Bird
Raised Fist0
Pandasdata~20 mins

Left join behavior in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Left Join Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this left join?

Given two pandas DataFrames df1 and df2:

df1 = pd.DataFrame({"key": [1, 2, 3], "val1": ["A", "B", "C"]})
df2 = pd.DataFrame({"key": [2, 3, 4], "val2": ["X", "Y", "Z"]})

result = df1.merge(df2, on="key", how="left")
print(result)

What will be printed?

Pandas
import pandas as pd
df1 = pd.DataFrame({"key": [1, 2, 3], "val1": ["A", "B", "C"]})
df2 = pd.DataFrame({"key": [2, 3, 4], "val2": ["X", "Y", "Z"]})

result = df1.merge(df2, on="key", how="left")
print(result)
A
   key val1 val2
0    1    A    Z
1    2    B    X
2    3    C    Y
B
   key val1 val2
0    1    A    X
1    2    B    Y
2    3    C    Z
C
   key val1 val2
0    1    A  NaN
1    2    B  NaN
2    3    C  NaN
D
   key val1 val2
0    1    A  NaN
1    2    B    X
2    3    C    Y
Attempts:
2 left
💡 Hint

Remember, a left join keeps all rows from the left DataFrame and matches rows from the right DataFrame where keys are equal. If no match, NaN appears.

🧠 Conceptual
intermediate
1:30remaining
What happens to unmatched rows in a left join?

When performing a left join between two tables, what happens to rows in the left table that have no matching key in the right table?

AThey appear in the result with NULL or NaN values for the right table's columns.
BThey are dropped from the result.
CThey cause an error and stop the join.
DThey are duplicated for every row in the right table.
Attempts:
2 left
💡 Hint

Think about what a left join means: all rows from the left table must appear.

📝 Syntax
advanced
2:00remaining
Which code correctly performs a left join on columns with different names?

You have two DataFrames:

df1 = pd.DataFrame({"id": [1, 2], "val1": ["A", "B"]})
df2 = pd.DataFrame({"key": [2, 3], "val2": ["X", "Y"]})

Which code correctly performs a left join where df1.id matches df2.key?

Adf1.merge(df2, on=["id", "key"], how="left")
Bdf1.merge(df2, left_on="id", right_on="key", how="left")
Cdf1.merge(df2, on="id", how="left")
Ddf1.merge(df2, left_index=True, right_index=True, how="left")
Attempts:
2 left
💡 Hint

When column names differ, you must specify which columns to join on from each DataFrame.

❓ optimization
advanced
2:30remaining
How to optimize a left join on large DataFrames?

You have two very large DataFrames and want to perform a left join on a key column. Which approach can improve performance?

ASet the key columns as indexes before joining using <code>set_index</code>.
BConvert the DataFrames to lists and join manually with loops.
CUse <code>concat</code> instead of <code>merge</code>.
DDrop all columns except the key before joining.
Attempts:
2 left
💡 Hint

Indexes can speed up lookups during joins.

🔧 Debug
expert
3:00remaining
Why does this left join produce unexpected duplicated rows?

Given:

df1 = pd.DataFrame({"key": [1, 2, 2], "val1": ["A", "B", "C"]})
df2 = pd.DataFrame({"key": [2], "val2": ["X"]})

result = df1.merge(df2, on="key", how="left")
print(result)

The output is:

   key val1 val2
0    1    A  NaN
1    2    B    X
2    2    C    X

Why are there two rows with key=2 in the result?

ABecause the merge function was called with wrong parameters.
BBecause df2 has duplicate keys, causing multiple matches.
CBecause df1 has duplicate keys, the join repeats matching rows for each duplicate.
DBecause left join removes duplicates automatically.
Attempts:
2 left
💡 Hint

Think about how joins behave when the left table has repeated keys.

Practice

(1/5)
1. What does a left join do in pandas when merging two DataFrames?
easy
A. Removes all rows that do not have matches in either DataFrame.
B. Keeps all rows from the left DataFrame and adds matching rows from the right DataFrame.
C. Keeps all rows from the right DataFrame and adds matching rows from the left DataFrame.
D. Keeps only rows that match in both DataFrames.

Solution

  1. Step 1: Understand left join behavior

    A left join keeps all rows from the left DataFrame regardless of matches.
  2. Step 2: Check how matching rows are added

    Matching rows from the right DataFrame are added; unmatched right rows are ignored.
  3. Final Answer:

    Keeps all rows from the left DataFrame and adds matching rows from the right DataFrame. -> Option B
  4. Quick Check:

    Left join = all left rows + matching right rows [OK]
Hint: Left join keeps all left rows, adds matching right rows [OK]
Common Mistakes:
  • Confusing left join with inner join
  • Thinking unmatched right rows appear in output
  • Mixing up left and right join behavior
2. Which of the following is the correct syntax to perform a left join of DataFrames df1 and df2 on column 'id' in pandas?
easy
A. pd.merge(df1, df2, on='id', how='left')
B. pd.merge(df1, df2, on='id', how='inner')
C. df1.join(df2, on='id', how='right')
D. pd.concat([df1, df2], join='left')

Solution

  1. Step 1: Identify correct merge function and parameters

    Use pd.merge with 'on' for join column and 'how="left"' for left join.
  2. Step 2: Check each option's correctness

    pd.merge(df1, df2, on='id', how='left') uses correct function and parameters; others use wrong join type or method.
  3. Final Answer:

    pd.merge(df1, df2, on='id', how='left') -> Option A
  4. Quick Check:

    Left join syntax = pd.merge(..., how='left') [OK]
Hint: Use pd.merge with how='left' for left join [OK]
Common Mistakes:
  • Using how='inner' instead of 'left'
  • Using pd.concat instead of pd.merge
  • Confusing join methods like right join
3. Given these DataFrames:
df1 = pd.DataFrame({'id': [1, 2, 3], 'val1': ['A', 'B', 'C']})
df2 = pd.DataFrame({'id': [2, 3, 4], 'val2': ['X', 'Y', 'Z']})

What is the result of pd.merge(df1, df2, on='id', how='left')?
medium
A. [{'id':1,'val1':'A','val2':NaN},{'id':2,'val1':'B','val2':'X'},{'id':3,'val1':'C','val2':'Y'}]
B. [{'id':2,'val1':'B','val2':'X'},{'id':3,'val1':'C','val2':'Y'},{'id':4,'val1':NaN,'val2':'Z'}]
C. [{'id':1,'val1':'A','val2':'X'},{'id':2,'val1':'B','val2':'Y'},{'id':3,'val1':'C','val2':'Z'}]
D. [{'id':1,'val1':'A','val2':'Z'},{'id':2,'val1':'B','val2':'X'},{'id':3,'val1':'C','val2':'Y'}]

Solution

  1. Step 1: Identify left join behavior on 'id'

    All rows from df1 (ids 1,2,3) appear; matching df2 rows added.
  2. Step 2: Match rows and fill unmatched with NaN

    id=1 has no match in df2, so val2 is NaN; id=2 and 3 match with val2 'X' and 'Y'.
  3. Final Answer:

    [{'id':1,'val1':'A','val2':NaN},{'id':2,'val1':'B','val2':'X'},{'id':3,'val1':'C','val2':'Y'}] -> Option A
  4. Quick Check:

    Left join keeps all left rows, unmatched right columns = NaN [OK]
Hint: Left join keeps all left rows; unmatched right columns become NaN [OK]
Common Mistakes:
  • Including rows from right DataFrame not in left
  • Filling unmatched right columns with wrong values
  • Confusing left join with inner join output
4. You wrote this code:
pd.merge(df1, df2, on='id', how='left')

but got an error: KeyError: 'id'. What is the likely cause?
medium
A. The DataFrames have different lengths.
B. You used 'how="left"' instead of 'how="inner"'.
C. You forgot to import pandas as pd.
D. The 'id' column is missing in one or both DataFrames.

Solution

  1. Step 1: Understand KeyError meaning

    KeyError means the specified column 'id' does not exist in one or both DataFrames.
  2. Step 2: Check other options for relevance

    Join type or import issues cause different errors; DataFrame length does not cause KeyError.
  3. Final Answer:

    The 'id' column is missing in one or both DataFrames. -> Option D
  4. Quick Check:

    KeyError 'id' = missing column [OK]
Hint: KeyError on column means column missing in DataFrame [OK]
Common Mistakes:
  • Assuming join type causes KeyError
  • Ignoring import errors
  • Thinking DataFrame length affects merge keys
5. You have two DataFrames:
df1 = pd.DataFrame({'key': [1, 2, 2, 3], 'val1': ['A', 'B', 'C', 'D']})
df2 = pd.DataFrame({'key': [2, 2, 4], 'val2': ['X', 'Y', 'Z']})

You perform pd.merge(df1, df2, on='key', how='left'). How many rows will the result have?
hard
A. 5
B. 4
C. 6
D. 3

Solution

  1. Step 1: Count rows in left DataFrame

    df1 has 4 rows with keys [1, 2, 2, 3].
  2. Step 2: Understand left join with duplicate keys

    Each df1 row matches all df2 rows with the same key. Key=2 appears twice in df1 and twice in df2, creating 2x2=4 rows for key=2.
  3. Step 3: Calculate total rows

    Key=1 and key=3 have no matches, so they remain as single rows with NaN for val2. Total rows = 1 (key=1) + 4 (key=2 matches) + 1 (key=3) = 6.
  4. Final Answer:

    6 -> Option C
  5. Quick Check:

    Duplicates multiply rows in left join [OK]
Hint: Duplicates in keys multiply rows in left join output [OK]
Common Mistakes:
  • Ignoring duplicate keys causing row multiplication
  • Counting only unique keys instead of rows
  • Assuming unmatched keys are dropped
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard