0
0
Data Analysis Pythondata~20 mins

concat() for stacking DataFrames in Data Analysis Python - 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
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.