Bird
Raised Fist0
Data Analysis Pythondata~20 mins

concat() for stacking DataFrames in Data Analysis Python - 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
Output of vertical stacking with concat()
What is the output DataFrame after stacking df1 and df2 vertically using pd.concat([df1, df2])?
Data Analysis Python
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  B
0  1  3
1  2  4
NaN  5  7
NaN  6  8
B
   A  B
0  1  3
1  2  4
2  5  7
3  6  8
C
   A  B
0  1  3
1  2  4
0  5  7
1  6  8
D
   A  B
0  1  3
1  2  4
0  5  7
2  6  8
Attempts:
2 left
💡 Hint
Remember that concat keeps original indices by default when stacking vertically.
❓ Predict Output
intermediate
2:00remaining
Output of horizontal stacking with concat()
What is the output DataFrame after stacking df1 and df2 horizontally using pd.concat([df1, df2], axis=1)?
Data Analysis Python
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
result = pd.concat([df1, df2], axis=1)
print(result)
A
   A  B
0  1  3
1  2  4
0  NaN 3
B
   A  B
0  1  3
1  2  4
C
   A  B
0  1  3
1  2  4
2  NaN 4
D
   A  B
0  1  3
1  2  NaN
2  NaN 4
Attempts:
2 left
💡 Hint
Horizontal stacking aligns rows by index.
❓ data_output
advanced
2:00remaining
Result of concat() with ignore_index=True
What is the output DataFrame after stacking df1 and df2 vertically using pd.concat([df1, df2], ignore_index=True)?
Data Analysis Python
import pandas as pd

df1 = pd.DataFrame({'X': [10, 20], 'Y': [30, 40]})
df2 = pd.DataFrame({'X': [50, 60], 'Y': [70, 80]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)
A
    X   Y
0  10  30
1  20  40
2  NaN 70
3  NaN 80
B
    X   Y
0  10  30
1  20  40
0  50  70
1  60  80
C
    X   Y
0  10  30
1  20  40
2  50  NaN
3  60  NaN
D
    X   Y
0  10  30
1  20  40
2  50  70
3  60  80
Attempts:
2 left
💡 Hint
ignore_index resets the row indices to a continuous range.
🔧 Debug
advanced
2:00remaining
Identify the error in concat() usage
What error will this code raise when trying to concatenate df1 and df2 with mismatched columns without specifying join?
Data Analysis Python
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
result = pd.concat([df1, df2])
print(result)
ANo error, output has columns A and B with NaNs where missing
BValueError: columns overlap but no join specified
CKeyError: 'B' not found in df1
DTypeError: cannot concatenate DataFrames with different columns
Attempts:
2 left
💡 Hint
Check how concat handles different columns by default.
🚀 Application
expert
3:00remaining
Stacking DataFrames with different columns and resetting index
You have two DataFrames with different columns. Which option correctly stacks them vertically, includes all columns, fills missing values with NaN, and resets the index?
Data Analysis Python
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'B': [5, 6], 'C': [7, 8]})
Apd.concat([df1, df2], ignore_index=True)
Bpd.concat([df1, df2], axis=1, ignore_index=True)
Cpd.concat([df1, df2], join='inner', ignore_index=True)
Dpd.concat([df1, df2], join='outer', axis=1)
Attempts:
2 left
💡 Hint
Think about vertical stacking with all columns and resetting index.

Practice

(1/5)
1. What does the concat() function do when stacking DataFrames with axis=0?
easy
A. It sorts the DataFrames before stacking.
B. It stacks DataFrames horizontally by adding columns.
C. It stacks DataFrames vertically by adding rows.
D. It merges DataFrames based on matching column values.

Solution

  1. Step 1: Understand axis parameter in concat()

    The axis=0 means stacking along rows, so new rows are added below existing ones.
  2. Step 2: Differentiate vertical vs horizontal stacking

    Vertical stacking adds rows; horizontal stacking (axis=1) adds columns side by side.
  3. Final Answer:

    It stacks DataFrames vertically by adding rows. -> Option C
  4. Quick Check:

    axis=0 means vertical stacking [OK]
Hint: axis=0 adds rows, axis=1 adds columns [OK]
Common Mistakes:
  • Confusing axis=0 with horizontal stacking
  • Thinking concat merges like join
  • Assuming concat sorts data automatically
2. Which of the following is the correct syntax to stack two DataFrames df1 and df2 vertically and reset the index?
easy
A. pd.concat([df1, df2], axis=1, ignore_index=True)
B. pd.concat([df1, df2], axis=0, ignore_index=True)
C. pd.concat(df1, df2, axis=0, ignore_index=True)
D. pd.concat([df1, df2], axis=0)

Solution

  1. Step 1: Check the concat() function syntax

    Correct syntax requires a list of DataFrames as first argument, e.g., [df1, df2].
  2. Step 2: Confirm axis and ignore_index usage

    To stack vertically and reset index, use axis=0 and ignore_index=True.
  3. Final Answer:

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

    List + axis=0 + ignore_index=True = correct syntax [OK]
Hint: Use list of DataFrames and axis=0 for vertical concat [OK]
Common Mistakes:
  • Passing DataFrames without list brackets
  • Using axis=1 for vertical stacking
  • Forgetting ignore_index to reset row numbers
3. Given these DataFrames:
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'A': [5], 'B': [6]})
result = pd.concat([df1, df2], axis=0, ignore_index=True)
print(result)

What is the output?
medium
A. A B 0 1 3 1 2 4 2 5 6
B. A B 0 1 3 1 2 4 0 5 6
C. A B 0 1 3 1 2 4
D. Error due to missing axis parameter

Solution

  1. Step 1: Understand concat with axis=0 and ignore_index=True

    Rows from df2 are added below df1, and index resets from 0 to 2.
  2. Step 2: Combine rows and reset index

    Result has 3 rows with index 0,1,2 and columns A and B filled accordingly.
  3. Final Answer:

    A B 0 1 3 1 2 4 2 5 6 -> Option A
  4. Quick Check:

    concat vertical + ignore_index resets index [OK]
Hint: ignore_index=True resets row numbers after concat [OK]
Common Mistakes:
  • Expecting original index to remain
  • Confusing axis=0 with axis=1 output
  • Forgetting to pass DataFrames as list
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. Missing list brackets around DataFrames in concat.
B. axis=0 is invalid for concat.
C. DataFrames must have same columns to concat.
D. ignore_index=True is required.

Solution

  1. Step 1: Check concat() argument format

    concat() expects a list of DataFrames as first argument, but here two DataFrames are passed separately.
  2. Step 2: Identify correct usage

    Correct call is pd.concat([df1, df2], axis=0) with brackets around DataFrames.
  3. Final Answer:

    Missing list brackets around DataFrames in concat. -> Option A
  4. Quick Check:

    concat needs list of DataFrames [OK]
Hint: Always pass DataFrames as a list to concat() [OK]
Common Mistakes:
  • Passing DataFrames as separate arguments
  • Assuming axis=0 is invalid
  • Thinking ignore_index is mandatory
5. You have two DataFrames:
df1 = pd.DataFrame({'Name': ['Alice', 'Bob'], 'Score': [85, 90]})
df2 = pd.DataFrame({'Name': ['Charlie'], 'Score': [88]})

You want to stack them vertically but keep the original index from both DataFrames. Which code will do this correctly?
hard
A. pd.concat([df1, df2], axis=0, ignore_index=True)
B. pd.concat(df1, df2, axis=0)
C. pd.concat([df1, df2], axis=1)
D. pd.concat([df1, df2], axis=0)

Solution

  1. Step 1: Understand index behavior in concat()

    By default, concat keeps original indexes unless ignore_index=True is set.
  2. Step 2: Choose correct parameters

    To stack vertically and keep indexes, use axis=0 without ignore_index.
  3. Final Answer:

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

    Default concat keeps original index [OK]
Hint: Omit ignore_index to keep original indexes [OK]
Common Mistakes:
  • Using ignore_index=True when original index is needed
  • Using axis=1 for vertical stacking
  • Passing DataFrames without list brackets
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