0
0
Pandasdata~10 mins

concat() for stacking DataFrames in Pandas - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to stack two DataFrames vertically using pandas.

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.[1]([df1, df2])
print(result)
Drag options to blanks, or click blank then click option'
Amerge
Bjoin
Cconcat
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using merge instead of concat causes errors or unexpected joins.
Using append is deprecated for stacking multiple DataFrames.
2fill in blank
medium

Complete the code to stack two DataFrames horizontally (side by side) using pandas concat.

Pandas
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)
Drag options to blanks, or click blank then click option'
A-1
B0
C2
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 stacks vertically, not horizontally.
Using axis=2 or -1 causes errors.
3fill in blank
hard

Fix the error in the code to stack DataFrames vertically and reset the index.

Pandas
import pandas as pd

df1 = pd.DataFrame({'X': [10, 20]})
df2 = pd.DataFrame({'X': [30, 40]})

result = pd.concat([df1, df2], ignore_index=[1])
print(result)
Drag options to blanks, or click blank then click option'
AFalse
BTrue
CNone
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Using False keeps original indexes, causing duplicate index values.
Using None or 0 causes errors or unexpected behavior.
4fill in blank
hard

Fill both blanks to stack DataFrames vertically and add keys to identify each part.

Pandas
import pandas as pd

df1 = pd.DataFrame({'Val': [1, 2]})
df2 = pd.DataFrame({'Val': [3, 4]})

result = pd.concat([df1, df2], [1]=True, [2]=['first', 'second'])
print(result)
Drag options to blanks, or click blank then click option'
Akeys
Bignore_index
Caxis
Dsort
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'keys' and 'ignore_index' parameters.
Forgetting to reset index causes confusing output.
5fill in blank
hard

Fill all three blanks to stack DataFrames horizontally, join only common columns, and reset index.

Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df2 = pd.DataFrame({'B': [5, 6], 'C': [7, 8]})

result = pd.concat([df1, df2], axis=[1], join="[2]", ignore_index=[3])
print(result)
Drag options to blanks, or click blank then click option'
ATrue
B1
Cinner
Douter
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 stacks vertically, not horizontally.
Using join='outer' keeps all columns, not just common ones.
Not resetting index causes duplicate indexes.