0
0
Pandasdata~10 mins

Why combining DataFrames matters in Pandas - Test Your Understanding

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

Complete the code to combine 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]})
combined = pd.[1]([df1, df2])
print(combined)
Drag options to blanks, or click blank then click option'
Ajoin
Bmerge
Cappend
Dconcat
Attempts:
3 left
💡 Hint
Common Mistakes
Using merge instead of concat for vertical stacking.
Using join which is for columns.
Using append which is deprecated in recent pandas versions.
2fill in blank
medium

Complete the code to merge two DataFrames on column 'key'.

Pandas
import pandas as pd

df1 = pd.DataFrame({'key': ['K0', 'K1'], 'A': [1, 2]})
df2 = pd.DataFrame({'key': ['K0', 'K1'], 'B': [3, 4]})
merged = pd.merge(df1, df2, on=[1])
print(merged)
Drag options to blanks, or click blank then click option'
A'key'
B'A'
C'B'
D'index'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist in both DataFrames.
Using 'index' which is not a column name here.
3fill in blank
hard

Fix the error in the code to join two DataFrames on their indexes.

Pandas
import pandas as pd

df1 = pd.DataFrame({'A': [1, 2]}, index=['a', 'b'])
df2 = pd.DataFrame({'B': [3, 4]}, index=['a', 'b'])
joined = df1.[1](df2, how='inner')
print(joined)
Drag options to blanks, or click blank then click option'
Amerge
Bconcat
Cjoin
Dappend
Attempts:
3 left
💡 Hint
Common Mistakes
Using merge which requires specifying columns.
Using concat which stacks DataFrames.
Using append which adds rows.
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.

Pandas
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)
Blen(word) > 3
Cword
Dlen(word) < 3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself as value instead of its length.
Using wrong condition like less than 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.

Pandas
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 lowercase instead of uppercase for keys.
Using wrong condition or value.