Bird
Raised Fist0
Pandasdata~20 mins

Outer join behavior in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Outer Join Mastery
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 outer 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"]})

What is the result of pd.merge(df1, df2, on='key', how='outer')?

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 = pd.merge(df1, df2, on='key', how='outer')
print(result.sort_values('key').reset_index(drop=True))
A[{'key': 1, 'val1': 'A', 'val2': 'X'}, {'key': 2, 'val1': 'B', 'val2': 'Y'}, {'key': 3, 'val1': 'C', 'val2': 'Z'}]
B[{'key': 1, 'val1': 'A', 'val2': None}, {'key': 2, 'val1': 'B', 'val2': 'X'}, {'key': 3, 'val1': 'C', 'val2': 'Y'}, {'key': 4, 'val1': None, 'val2': 'Z'}]
C[{'key': 2, 'val1': 'B', 'val2': 'X'}, {'key': 3, 'val1': 'C', 'val2': 'Y'}]
D[{'key': 1, 'val1': 'A', 'val2': 'X'}, {'key': 4, 'val1': None, 'val2': 'Z'}]
Attempts:
2 left
💡 Hint

Outer join keeps all keys from both DataFrames, filling missing values with None.

❓ query_result
intermediate
2:00remaining
How many rows after outer join with duplicate keys?

Consider these DataFrames with duplicate keys:

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

How many rows will pd.merge(df1, df2, on='key', how='outer') have?

Pandas
import pandas as pd
df1 = pd.DataFrame({"key": [1, 2, 2], "val1": ["A", "B", "C"]})
df2 = pd.DataFrame({"key": [2, 2, 3], "val2": ["X", "Y", "Z"]})
result = pd.merge(df1, df2, on='key', how='outer')
print(len(result))
A5
B7
C6
D4
Attempts:
2 left
💡 Hint

Remember that duplicates cause a Cartesian product for matching keys.

📝 Syntax
advanced
2:00remaining
Which code snippet correctly performs an outer join on two DataFrames?

Choose the code that correctly performs an outer join on df1 and df2 using pandas.

Apd.merge(df1, df2, on='key', how='outer')
Bdf1.merge(df2, how='outer')
Cpd.concat([df1, df2], join='outer')
Ddf1.join(df2, how='outer')
Attempts:
2 left
💡 Hint

Check the correct function and parameters for outer join in pandas.

🔧 Debug
advanced
2:00remaining
Why does this outer join produce unexpected NaNs?

Given these DataFrames:

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

Why does pd.merge(df1, df2, on='key', how='outer') produce NaNs in all columns except the keys?

Pandas
import pandas as pd
df1 = pd.DataFrame({"key": [1, 2], "val1": ["A", "B"]})
df2 = pd.DataFrame({"key": ["1", "2"], "val2": ["X", "Y"]})
result = pd.merge(df1, df2, on='key', how='outer')
print(result)
ABecause the key columns have different data types (int vs string), so no matches occur.
BBecause outer join only keeps rows with matching keys, so unmatched rows get NaN.
CBecause the DataFrames have different column names, causing merge to fail silently.
DBecause the merge function requires specifying left_on and right_on for different column names.
Attempts:
2 left
💡 Hint

Check the data types of the key columns in both DataFrames.

🧠 Conceptual
expert
2:00remaining
What is the effect of outer join on index alignment in pandas?

When performing an outer join with pd.merge on two DataFrames with overlapping and non-overlapping keys, how does pandas handle the index of the resulting DataFrame?

AThe resulting DataFrame uses a MultiIndex combining indexes from both DataFrames.
BThe resulting DataFrame preserves the index from the left DataFrame only.
CThe resulting DataFrame preserves the index from the right DataFrame only.
DThe resulting DataFrame has a new default integer index from 0 to n-1, ignoring original indexes.
Attempts:
2 left
💡 Hint

Think about how pandas resets or preserves indexes after merge.

Practice

(1/5)
1. What does an outer join do when merging two DataFrames in pandas?
easy
A. Keeps only rows from the right DataFrame
B. Keeps only rows that match in both DataFrames
C. Keeps all rows from both DataFrames, filling missing values with NaN
D. Keeps only rows from the left DataFrame

Solution

  1. Step 1: Understand the join types in pandas

    Inner join keeps only matching rows, left join keeps all from left, right join keeps all from right, outer join keeps all from both.
  2. Step 2: Identify behavior of outer join

    Outer join includes all rows from both DataFrames and fills missing matches with NaN.
  3. Final Answer:

    Keeps all rows from both DataFrames, filling missing values with NaN -> Option C
  4. Quick Check:

    Outer join = all rows kept with NaN for missing [OK]
Hint: Outer join keeps all rows, fills missing with NaN [OK]
Common Mistakes:
  • Confusing outer join with inner join
  • Thinking outer join keeps only left or right rows
  • Assuming missing data is dropped instead of NaN
2. Which of the following is the correct syntax to perform an outer join on DataFrames df1 and df2 using pandas?
easy
A. pd.merge(df1, df2, how='outer')
B. pd.merge(df1, df2, join='outer')
C. df1.join(df2, join='outer')
D. df1.merge(df2, type='outer')

Solution

  1. Step 1: Recall the pandas merge syntax

    The function pd.merge() takes parameters including left DataFrame, right DataFrame, and how to join.
  2. Step 2: Identify correct parameter for outer join

    The parameter to specify join type is how='outer'. Other options like join='outer' or type='outer' are invalid.
  3. Final Answer:

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

    Use how='outer' in pd.merge() [OK]
Hint: Use how='outer' in pd.merge() for outer join [OK]
Common Mistakes:
  • Using join='outer' instead of how='outer'
  • Using type='outer' which is invalid
  • Using df1.join() without specifying correct parameters
3. Given the following DataFrames:
df1 = pd.DataFrame({'key': ['A', 'B'], 'val1': [1, 2]})
df2 = pd.DataFrame({'key': ['B', 'C'], 'val2': [3, 4]})

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

Solution

  1. Step 1: Identify keys and values in both DataFrames

    df1 keys: A, B with val1 1, 2; df2 keys: B, C with val2 3, 4.
  2. Step 2: Perform outer join on 'key'

    Outer join keeps all keys: A, B, C. For A val2 is missing (NaN), for C val1 is missing (NaN), B has both values.
  3. Final Answer:

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

    Outer join keeps all keys with NaN for missing values [OK]
Hint: Outer join keys = union of keys, missing values become NaN [OK]
Common Mistakes:
  • Confusing outer join with inner join (only B)
  • Missing keys from either DataFrame
  • Not filling missing values with NaN
4. You wrote this code but got an error:
pd.merge(df1, df2, how='outer', on='key', left_index=True)

What is the likely cause of the error?
medium
A. Cannot use both 'on' and 'left_index=True' together in merge
B. Missing 'right_index=True' parameter
C. The 'how' parameter should be 'outerjoin' not 'outer'
D. DataFrames must have the same columns to merge

Solution

  1. Step 1: Understand parameters 'on' and 'left_index'

    'on' specifies column to join on; 'left_index=True' uses index for join. Both cannot be used simultaneously.
  2. Step 2: Identify conflict causing error

    Using 'on' and 'left_index=True' together causes pandas to raise an error because join keys are ambiguous.
  3. Final Answer:

    Cannot use both 'on' and 'left_index=True' together in merge -> Option A
  4. Quick Check:

    'on' and 'left_index=True' conflict in merge [OK]
Hint: Don't mix 'on' with 'left_index=True' in merge [OK]
Common Mistakes:
  • Assuming 'how'='outerjoin' is valid
  • Forgetting that 'on' and 'left_index' conflict
  • Thinking DataFrames must have same columns to merge
5. You have two DataFrames:
df1 = pd.DataFrame({'id': [1, 2, 3], 'score': [10, 20, 30]})
df2 = pd.DataFrame({'id': [2, 3, 4], 'grade': ['B', 'A', 'C']})

You want to merge them with an outer join but replace all NaN in 'score' with 0 and in 'grade' with 'N/A'. Which code correctly does this?
hard
A. result = pd.merge(df1, df2, on='id', how='outer').fillna({'score': 0})
B. result = pd.merge(df1, df2, on='id', how='outer').fillna(0).fillna('N/A')
C. result = pd.merge(df1, df2, on='id', how='outer').fillna({'score': 'N/A', 'grade': 0})
D. result = pd.merge(df1, df2, on='id', how='outer').fillna({'score': 0, 'grade': 'N/A'})

Solution

  1. Step 1: Perform outer join on 'id'

    Outer join keeps all ids: 1, 2, 3, 4. Missing 'score' or 'grade' will be NaN.
  2. Step 2: Replace NaN with correct values per column

    Use fillna with a dictionary to specify 0 for 'score' and 'N/A' for 'grade'. Other options either fill all with 0 or wrong values.
  3. Final Answer:

    result = pd.merge(df1, df2, on='id', how='outer').fillna({'score': 0, 'grade': 'N/A'}) -> Option D
  4. Quick Check:

    Use fillna with dict for column-specific NaN replacement [OK]
Hint: Use fillna({'col1': val1, 'col2': val2}) for different columns [OK]
Common Mistakes:
  • Using fillna(0) for all columns, replacing strings incorrectly
  • Swapping fillna values between columns
  • Not filling NaN for all missing columns
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