0
0
Pandasdata~20 mins

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

Choose your learning style9 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'.