0
0
Data Analysis Pythondata~10 mins

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

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], 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=1 stacks DataFrames side by side (horizontally).
For vertical stacking, axis must be 0.
2fill in blank
medium

Complete the code to reset the index after stacking DataFrames vertically.

Data Analysis Python
import pandas as pd

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

stacked = pd.concat([df1, df2], axis=0)
stacked_reset = stacked.[1](drop=True)
print(stacked_reset)
Drag options to blanks, or click blank then click option'
Areset_index
Breindex
Cset_index
Dsort_index
Attempts:
3 left
💡 Hint
Common Mistakes
Using reindex does not reset the index numbering.
For resetting index, use reset_index.
3fill in blank
hard

Fix the error in the code to stack DataFrames horizontally.

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)
Drag options to blanks, or click blank then click option'
A-1
B2
C0
D1
Attempts:
3 left
💡 Hint
Common Mistakes
Using axis=0 stacks vertically, not horizontally.
Axis must be 1 for horizontal stacking.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that maps words to their lengths only if length is greater than 3.

Data Analysis Python
words = ['apple', 'bat', 'car', 'door']
lengths = {word: [1] for word in words if [2]
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Clen(word) > 3
Dword > 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Incorrect condition like comparing word to 3.
5fill in blank
hard

Fill all three blanks to create a dictionary comprehension that maps uppercase words to their lengths only if length is greater than 3.

Data Analysis Python
words = ['apple', 'bat', 'car', 'door']
result = { [1]: [2] for word in words if [3] }
print(result)
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Clen(word) > 3
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Using the original word as key instead of uppercase.
Not filtering words by length.
Using incorrect functions for keys or values.