0
0
Data Analysis Pythondata~20 mins

Melt for wide-to-long reshaping in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Melt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this melt operation?
Given the DataFrame below, what will be the result of melting it with id_vars=['Name'] and var_name='Year'?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({
    'Name': ['Alice', 'Bob'],
    '2019': [85, 90],
    '2020': [88, 92]
})

melted = pd.melt(df, id_vars=['Name'], var_name='Year', value_name='Score')
print(melted)
A[{'Name': 'Alice', 'Year': '2019', 'Score': 85}, {'Name': 'Alice', 'Year': '2020', 'Score': 88}, {'Name': 'Bob', 'Year': '2019', 'Score': 90}, {'Name': 'Bob', 'Year': '2020', 'Score': 92}]
B[{'Name': 'Alice', 'Year': '2019', 'Score': 85}, {'Name': 'Bob', 'Year': '2019', 'Score': 90}, {'Name': 'Alice', 'Year': '2020', 'Score': 88}, {'Name': 'Bob', 'Year': '2020', 'Score': 92}]
C[{'Name': 'Alice', 'Year': '2019', 'Score': 85}, {'Name': 'Bob', 'Year': '2019', 'Score': 90}]
D[{'Name': 'Alice', 'Year': '2019', 'Score': 85}, {'Name': 'Bob', 'Year': '2020', 'Score': 92}]
Attempts:
2 left
💡 Hint
Remember that melt stacks columns vertically, keeping id_vars fixed.
data_output
intermediate
1:00remaining
How many rows after melting?
If you melt a DataFrame with 3 rows and 4 value columns (excluding id_vars), how many rows will the melted DataFrame have?
A7
B12
C3
D4
Attempts:
2 left
💡 Hint
Each original row expands into one row per melted column.
🔧 Debug
advanced
1:30remaining
Identify the error in this melt code
What error will this code raise?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'A': [1,2], 'B': [3,4]})

melted = pd.melt(df, id_vars=['C'])
print(melted)
AValueError: No var_name specified
BTypeError: id_vars must be a list
CKeyError: 'C'
DNo error, prints melted DataFrame
Attempts:
2 left
💡 Hint
Check if 'C' exists as a column in df.
🚀 Application
advanced
2:00remaining
Using melt to reshape survey data
You have a DataFrame with columns: 'Respondent', 'Q1', 'Q2', 'Q3'. You want a long format with columns: 'Respondent', 'Question', 'Answer'. Which melt call achieves this?
Apd.melt(df, id_vars=['Answer'], var_name='Question', value_name='Respondent')
Bpd.melt(df, id_vars=['Respondent'], var_name='Answer', value_name='Question')
Cpd.melt(df, id_vars=['Question'], var_name='Respondent', value_name='Answer')
Dpd.melt(df, id_vars=['Respondent'], var_name='Question', value_name='Answer')
Attempts:
2 left
💡 Hint
id_vars should be the column to keep fixed, var_name the new column for old column names.
🧠 Conceptual
expert
2:30remaining
Why use melt instead of stack for wide-to-long reshaping?
Which statement best explains why melt is preferred over stack for reshaping DataFrames with multiple value columns?
AMelt allows specifying id_vars and custom column names, making it more flexible for complex reshaping.
BStack only works on Series, not DataFrames, so melt is the only option.
CStack automatically sorts data alphabetically, which can cause data loss.
DMelt is faster because it uses less memory than stack.
Attempts:
2 left
💡 Hint
Think about control over column names and identifiers.