Challenge - 5 Problems
Concat Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of concat equivalent to append
What is the output of this code that uses
pd.concat to append one DataFrame to another?Pandas
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], ignore_index=True) print(result)
Attempts:
2 left
💡 Hint
Think about how concat stacks DataFrames vertically and what ignore_index=True does.
✗ Incorrect
pd.concat stacks the two DataFrames vertically and resets the index with ignore_index=True, so the new row from df2 is added at the bottom with a new index.
❓ data_output
intermediate1:00remaining
Number of rows after concat equivalent to append
Given two DataFrames
df1 with 3 rows and df2 with 2 rows, what is the number of rows in pd.concat([df1, df2])?Attempts:
2 left
💡 Hint
Concat stacks rows from both DataFrames.
✗ Incorrect
Concat combines all rows from both DataFrames, so total rows = 3 + 2 = 5.
🔧 Debug
advanced2:00remaining
Why does this concat code raise an error?
This code raises an error. What is the cause?
import pandas as pd
df1 = pd.DataFrame({'A': [1, 2]})
df2 = pd.DataFrame({'B': [3, 4]})
result = pd.concat([df1, df2], ignore_index=True)
print(result)Attempts:
2 left
💡 Hint
Try running the code to see what happens when columns differ.
✗ Incorrect
pd.concat stacks rows and aligns columns by name. Missing columns get NaN values. No error is raised.
🚀 Application
advanced2:30remaining
Using concat to append with keys
How can you use
pd.concat to append two DataFrames and add keys to identify their origin?Pandas
import pandas as pd df1 = pd.DataFrame({'X': [1, 2]}) df2 = pd.DataFrame({'X': [3, 4]}) result = pd.concat([df1, df2], keys=['first', 'second']) print(result)
Attempts:
2 left
💡 Hint
Keys create a hierarchical index to label rows from each DataFrame.
✗ Incorrect
The keys parameter creates a MultiIndex where the first level is the key label for each DataFrame's rows.
🧠 Conceptual
expert1:30remaining
Why is concat preferred over append in pandas 1.4+?
Pandas deprecated the
append method for DataFrames. Why is pd.concat preferred instead?Attempts:
2 left
💡 Hint
Think about performance and flexibility when combining many DataFrames.
✗ Incorrect
Concat can combine many DataFrames efficiently in one call, while append is slower and less flexible.