Challenge - 5 Problems
Concat Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
By default, concat keeps the original indices when stacking vertically.
✗ Incorrect
When stacking vertically with pd.concat, the rows from df2 keep their original indices, so the result has duplicate indices 0 and 1.
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
ignore_index=True resets the index to a continuous range.
✗ Incorrect
Setting ignore_index=True makes concat reset the index so the rows are numbered 0 to n-1 in the result.
❓ data_output
advanced2: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)
Attempts:
2 left
💡 Hint
Concat with axis=1 stacks columns side by side, aligning by index.
✗ Incorrect
The result has 3 rows (from df1) and 2 columns (A and B). Missing values fill where df2 has fewer rows.
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
join='inner' keeps only columns present in all DataFrames.
✗ Incorrect
When concatenating vertically with join='inner', pandas keeps only columns common to all DataFrames. Here, no columns are common, so the result is empty with correct shape.
🚀 Application
expert3: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]})
Attempts:
2 left
💡 Hint
To keep all columns when stacking vertically, use join='outer'.
✗ Incorrect
join='outer' keeps all columns from all DataFrames, filling missing values with NaN. axis=0 stacks rows vertically.