Bird
Raised Fist0
Data Analysis Pythondata~20 mins

Outer join in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Outer 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 outer join?

Given two dataframes df1 and df2:

df1 = pd.DataFrame({"id": [1, 2, 3], "value1": [10, 20, 30]})
df2 = pd.DataFrame({"id": [2, 3, 4], "value2": [200, 300, 400]})

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

What will be printed?

Data Analysis Python
import pandas as pd
df1 = pd.DataFrame({"id": [1, 2, 3], "value1": [10, 20, 30]})
df2 = pd.DataFrame({"id": [2, 3, 4], "value2": [200, 300, 400]})
result = pd.merge(df1, df2, on="id", how="left")
print(result)
A
   id  value1  value2
0   1      10   100.0
1   2      20   200.0
2   3      30   300.0
B
   id  value1  value2
0   1      10     NaN
1   2      20   200.0
2   3      30   300.0
C
   id  value1  value2
0   1      10   200.0
1   2      20   300.0
2   3      30   400.0
D
   id  value1  value2
0   2      20   200.0
1   3      30   300.0
2   4     NaN   400.0
Attempts:
2 left
💡 Hint

Remember, a left join keeps all rows from the left dataframe and matches rows from the right dataframe where possible.

❓ query_result
intermediate
2:00remaining
What is the output of this full outer join?

Given two dataframes df1 and df2:

df1 = pd.DataFrame({"key": ["A", "B", "C"], "val1": [1, 2, 3]})
df2 = pd.DataFrame({"key": ["B", "C", "D"], "val2": [20, 30, 40]})

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

What will be printed?

Data Analysis Python
import pandas as pd
df1 = pd.DataFrame({"key": ["A", "B", "C"], "val1": [1, 2, 3]})
df2 = pd.DataFrame({"key": ["B", "C", "D"], "val2": [20, 30, 40]})
result = pd.merge(df1, df2, on="key", how="outer")
print(result)
A
  key  val1  val2
0   A   1.0   NaN
1   B   2.0  20.0
2   C   3.0  30.0
3   D   NaN  40.0
B
  key  val1  val2
0   A   1.0  20.0
1   B   2.0  30.0
2   C   3.0  40.0
C
  key  val1  val2
0   B   2.0  20.0
1   C   3.0  30.0
2   D   NaN  40.0
D
  key  val1  val2
0   A   NaN   NaN
1   B   2.0  20.0
2   C   3.0  30.0
3   D   40.0  40.0
Attempts:
2 left
💡 Hint

Full outer join keeps all rows from both dataframes, filling missing values with NaN.

📝 Syntax
advanced
1:30remaining
Which option produces a syntax error in this outer join code?

Consider the following code snippet to perform an outer join in pandas:

pd.merge(df1, df2, on='id', how='outer')

Which of the following options will cause a syntax error?

Data Analysis Python
import pandas as pd
pd.merge(df1, df2, on='id', how='outer')
Apd.merge(df1, df2, on='id', how='outer')
B)'retuo'=woh ,'di'=no ,2fd ,1fd(egrem.dp
Cpd.merge(df1, df2, on='id' how='outer')
Dd.merge(df1, df2, on='id', how='outer')
Attempts:
2 left
💡 Hint

Check for missing commas between arguments.

❓ optimization
advanced
2:30remaining
Which option is the most efficient way to perform a full outer join on large dataframes?

You have two large dataframes df1 and df2 with millions of rows. You want to perform a full outer join on column key. Which option is the most efficient?

Apd.merge(df1, df2, on='key', how='inner', sort=False)
Bpd.merge(df1, df2, on='key', how='outer', sort=True)
Cpd.merge(df1, df2, on='key', how='outer').sort_values('key')
Dpd.merge(df1, df2, on='key', how='outer', sort=False)
Attempts:
2 left
💡 Hint

Sorting during merge can slow down performance on large data.

🧠 Conceptual
expert
2:00remaining
What is the number of rows in the result of a full outer join?

Given two tables T1 and T2 with unique keys, the number of rows in a full outer join on the key column is:

AThe sum of the number of rows in <code>T1</code> and <code>T2</code> minus the number of keys common to both
BThe number of rows in <code>T1</code> only
CThe number of rows in <code>T2</code> only
DThe product of the number of rows in <code>T1</code> and <code>T2</code>
Attempts:
2 left
💡 Hint

Think about how full outer join combines all unique keys from both tables.

Practice

(1/5)
1. What does an outer join do when combining two tables?
easy
A. Keeps all rows from both tables, filling missing matches with nulls
B. Keeps only rows that match in both tables
C. Keeps only rows from the first table
D. Keeps only rows from the second table

Solution

  1. Step 1: Understand join types

    Inner join keeps only matching rows, outer join keeps all rows from both tables.
  2. Step 2: Define outer join behavior

    Outer join fills missing matches with null (or NaN) values to keep all data.
  3. Final Answer:

    Keeps all rows from both tables, filling missing matches with nulls -> Option A
  4. Quick Check:

    Outer join = all rows kept with nulls for missing [OK]
Hint: Outer join keeps all rows from both tables with nulls for missing [OK]
Common Mistakes:
  • Confusing outer join with inner join
  • Thinking outer join keeps only one table's rows
  • Assuming missing matches are dropped
2. Which of the following is the correct syntax to perform a full outer join using pandas?
easy
A. pd.merge(df1, df2, how='outer')
B. pd.join(df1, df2, type='full')
C. df1.outer_join(df2)
D. pd.merge(df1, df2, how='fulljoin')

Solution

  1. Step 1: Recall pandas merge syntax

    Use pd.merge() with how='outer' for full outer join.
  2. Step 2: Check options for correctness

    pd.merge(df1, df2, how='outer') matches correct syntax; others use invalid methods or parameters.
  3. Final Answer:

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

    pandas full outer join = merge with how='outer' [OK]
Hint: Use pd.merge with how='outer' for full outer join in pandas [OK]
Common Mistakes:
  • Using pd.join instead of pd.merge for outer join
  • Wrong parameter names like 'fulljoin' or 'type'
  • Calling non-existent methods like outer_join
3. Given these two DataFrames:
df1 = pd.DataFrame({'id': [1, 2], 'val1': ['A', 'B']})
df2 = pd.DataFrame({'id': [2, 3], 'val2': ['X', 'Y']})

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

Solution

  1. Step 1: Identify keys and join type

    Joining on 'id' with full outer join keeps all ids from both DataFrames.
  2. Step 2: Combine rows with matching and missing data

    id=1 only in df1, id=2 in both, id=3 only in df2; missing values become NaN.
  3. Final Answer:

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

    Outer join keeps all ids with NaN for missing columns [OK]
Hint: Outer join keeps all keys, fills missing columns with NaN [OK]
Common Mistakes:
  • Expecting only matching rows (inner join)
  • Ignoring NaN for missing matches
  • Confusing left or right join results
4. What is wrong with this code snippet for an outer join?
result = pd.merge(df1, df2, on='key', how='outside')
medium
A. df1 and df2 must have the same columns
B. The 'on' parameter should be a list, not a string
C. pd.merge cannot perform outer joins
D. The 'how' parameter value 'outside' is invalid

Solution

  1. Step 1: Check 'how' parameter values

    Valid values are 'left', 'right', 'inner', 'outer'; 'outside' is invalid.
  2. Step 2: Validate other parameters

    'on' can be string or list; pd.merge supports outer joins; columns can differ.
  3. Final Answer:

    The 'how' parameter value 'outside' is invalid -> Option D
  4. Quick Check:

    Invalid join type causes error [OK]
Hint: Check 'how' parameter spelling for join type [OK]
Common Mistakes:
  • Using invalid join type strings
  • Thinking 'on' must be a list always
  • Assuming pd.merge can't do outer joins
5. You have two DataFrames:
df1 = pd.DataFrame({'id': [1, 2, 2], 'score': [10, 20, 20]})
df2 = pd.DataFrame({'id': [2, 3], 'grade': ['A', 'B']})

You want to perform a full outer join on 'id' and get all combinations of matching rows (including duplicates). Which code correctly achieves this?
hard
A. pd.merge(df1, df2, on='id', how='outer').drop_duplicates()
B. pd.merge(df1, df2, on='id', how='outer')
C. pd.merge(df1, df2, on='id', how='inner')
D. pd.merge(df1, df2, on='id', how='outer', indicator=True)

Solution

  1. Step 1: Understand join type needed

    Full outer join keeps all rows from both DataFrames, including duplicates.
  2. Step 2: Check code options for duplicates

    pd.merge(df1, df2, on='id', how='outer') keeps duplicates; pd.merge(df1, df2, on='id', how='outer').drop_duplicates() removes duplicates; pd.merge(df1, df2, on='id', how='inner') is inner join; pd.merge(df1, df2, on='id', how='outer', indicator=True) adds indicator but doesn't affect duplicates.
  3. Final Answer:

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

    Full outer join keeps duplicates unless dropped [OK]
Hint: Full outer join keeps duplicates; don't drop them unless needed [OK]
Common Mistakes:
  • Using inner join instead of outer join
  • Dropping duplicates unintentionally
  • Confusing indicator parameter with join behavior
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