Bird
Raised Fist0
Pandasdata~20 mins

concat() for stacking DataFrames in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Concat Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of vertical stacking with concat()?
Given two DataFrames df1 and df2, what is the output of pd.concat([df1, df2])?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result = pd.concat([df1, df2])
print(result)
A{'A': [1, 2], 'B': [3, 4]} only
B{'A': [1, 2, 5, 6], 'B': [3, 4, 7, 8]} with index [0, 1, 0, 1]
C{'A': [1, 2, 5, 6], 'B': [3, 4, 7, 8]} with index [0, 1, 2, 3]
DRaises a KeyError
Attempts:
2 left
💡 Hint
By default, concat keeps the original indices when stacking vertically.
❓ Predict Output
intermediate
2:00remaining
What does the 'ignore_index=True' option do in concat()?
What is the output of pd.concat([df1, df2], ignore_index=True) given the same df1 and df2 as before?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)
A{'A': [1, 2, 5, 6], 'B': [3, 4, 7, 8]} with index [0, 1, 2, 3]
BRaises a TypeError
C{'A': [1, 2, 5, 6], 'B': [3, 4, 7, 8]} with index [0, 1, 0, 1]
DReturns an empty DataFrame
Attempts:
2 left
💡 Hint
ignore_index=True resets the index to a continuous range.
❓ data_output
advanced
2:00remaining
What is the shape of the DataFrame after horizontal concat?
Given df1 and df2 below, what is the shape of the DataFrame after pd.concat([df1, df2], axis=1)?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2, 3]})
df2 = pd.DataFrame({'B': [4, 5]})
result = pd.concat([df1, df2], axis=1)
print(result.shape)
A(3, 1)
B(2, 2)
C(3, 2)
D(2, 1)
Attempts:
2 left
💡 Hint
Concat with axis=1 stacks columns side by side, aligning by index.
🔧 Debug
advanced
2:00remaining
Why does this concat code raise an error?
What error does the following code raise and why?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
result = pd.concat([df1, df2], axis=0, join='inner')
print(result)
ARuns successfully and returns only the common columns
BRaises a TypeError because join must be a boolean
CRaises a ValueError because join='inner' is invalid with axis=0
DRaises a KeyError due to missing columns
Attempts:
2 left
💡 Hint
join='inner' keeps only columns present in all DataFrames.
🚀 Application
expert
3:00remaining
How to stack multiple DataFrames with different columns and keep all data?
You have three DataFrames with different columns. Which concat call stacks them vertically and keeps all columns, filling missing values with NaN?
Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1], 'B': [2]})
df2 = pd.DataFrame({'B': [3], 'C': [4]})
df3 = pd.DataFrame({'A': [5], 'C': [6]})
Apd.concat([df1, df2, df3], axis=0, join='inner')
Bpd.concat([df1, df2, df3], axis=1, join='inner')
Cpd.concat([df1, df2, df3], axis=1, join='outer')
Dpd.concat([df1, df2, df3], axis=0, join='outer')
Attempts:
2 left
💡 Hint
To keep all columns when stacking vertically, use join='outer'.

Practice

(1/5)
1. What does the concat() function do in pandas?
easy
A. Deletes rows from a DataFrame
B. Joins DataFrames by stacking rows or columns
C. Sorts the DataFrame by index
D. Filters rows based on a condition

Solution

  1. Step 1: Understand the purpose of concat()

    concat() is used to combine multiple DataFrames either by stacking rows or columns.
  2. Step 2: Compare with other DataFrame operations

    Deleting, sorting, or filtering are different operations and not related to concat().
  3. Final Answer:

    Joins DataFrames by stacking rows or columns -> Option B
  4. Quick Check:

    concat() stacks DataFrames [OK]
Hint: Remember: concat stacks rows or columns [OK]
Common Mistakes:
  • Confusing concat with filtering or sorting
  • Thinking concat deletes data
  • Assuming concat only merges columns
2. Which of the following is the correct syntax to stack two DataFrames df1 and df2 vertically using concat()?
easy
A. pd.concat(df1 + df2)
B. pd.concat([df1, df2], axis=1)
C. pd.concat(df1, df2)
D. pd.concat([df1, df2], axis=0)

Solution

  1. Step 1: Identify axis for vertical stacking

    Vertical stacking means stacking rows, which is done with axis=0.
  2. Step 2: Check syntax correctness

    pd.concat() requires a list of DataFrames as first argument, so [df1, df2] is correct.
  3. Final Answer:

    pd.concat([df1, df2], axis=0) -> Option D
  4. Quick Check:

    Vertical stack uses axis=0 [OK]
Hint: Use axis=0 to stack rows vertically [OK]
Common Mistakes:
  • Using axis=1 for vertical stacking
  • Passing DataFrames without a list
  • Trying to add DataFrames with + inside concat
3. Given the following code:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5, 6], 'B': [7, 8]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)

What is the output?
medium
A. DataFrame with 4 rows, original indexes 0,1,0,1
B. DataFrame with 2 rows, columns stacked horizontally
C. DataFrame with 4 rows, index reset from 0 to 3
D. Error because ignore_index is invalid

Solution

  1. Step 1: Understand concat with ignore_index=True

    Concatenating df1 and df2 vertically stacks rows, resulting in 4 rows total. ignore_index=True resets the index to 0,1,2,3.
  2. Step 2: Confirm output structure

    The columns remain the same ('A' and 'B'), rows are stacked, and index is reset.
  3. Final Answer:

    DataFrame with 4 rows, index reset from 0 to 3 -> Option C
  4. Quick Check:

    ignore_index=True resets index [OK]
Hint: ignore_index=True resets row numbers after stacking [OK]
Common Mistakes:
  • Forgetting ignore_index resets index
  • Thinking columns stack horizontally by default
  • Assuming error with ignore_index parameter
4. What is wrong with this code snippet?
import pandas as pd
df1 = pd.DataFrame({'X': [1, 2]})
df2 = pd.DataFrame({'Y': [3, 4]})
result = pd.concat(df1, df2, axis=0)
print(result)
medium
A. pd.concat requires a list of DataFrames as first argument
B. axis=0 stacks columns instead of rows
C. DataFrames must have same columns to concat
D. ignore_index=True is missing

Solution

  1. Step 1: Check pd.concat argument format

    pd.concat() expects a list or iterable of DataFrames as the first argument, but here two DataFrames are passed as separate arguments.
  2. Step 2: Identify correct usage

    The correct call is pd.concat([df1, df2], axis=0) with a list.
  3. Final Answer:

    pd.concat requires a list of DataFrames as first argument -> Option A
  4. Quick Check:

    pd.concat([df1, df2]) needs list [OK]
Hint: Always pass DataFrames as a list to concat [OK]
Common Mistakes:
  • Passing DataFrames as separate arguments
  • Confusing axis parameter meaning
  • Assuming columns must match exactly
5. You have two DataFrames:
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'B': [5, 6], 'C': [7, 8]})

You want to stack them vertically and keep all columns, filling missing values with NaN. Which code will do this correctly?
hard
A. pd.concat([df1, df2], axis=0, ignore_index=True)
B. pd.concat([df1, df2], axis=1)
C. pd.concat([df1, df2], axis=0).fillna(0)
D. pd.concat([df1, df2], axis=0, join='inner')

Solution

  1. Step 1: Understand vertical stacking with different columns

    Stacking vertically with axis=0 combines rows. Since columns differ, missing columns get NaN by default.
  2. Step 2: Check parameters for desired behavior

    ignore_index=True resets index. Default join='outer' keeps all columns and fills missing with NaN.
  3. Step 3: Evaluate other options

    pd.concat([df1, df2], axis=1) stacks columns horizontally, not vertically. pd.concat([df1, df2], axis=0).fillna(0) fills NaN with 0, not requested. pd.concat([df1, df2], axis=0, join='inner') uses join='inner' which keeps only common columns.
  4. Final Answer:

    pd.concat([df1, df2], axis=0, ignore_index=True) -> Option A
  5. Quick Check:

    axis=0 + default join='outer' keeps all columns [OK]
Hint: Use axis=0 and default join to keep all columns [OK]
Common Mistakes:
  • Using axis=1 to stack rows
  • Using join='inner' and losing columns
  • Filling NaN when not needed
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