Challenge - 5 Problems
Melt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Remember that melt stacks columns vertically, keeping id_vars fixed.
✗ Incorrect
Melt transforms columns '2019' and '2020' into rows under 'Year', keeping 'Name' as identifier. The order is by original rows then columns melted.
❓ data_output
intermediate1: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?
Attempts:
2 left
💡 Hint
Each original row expands into one row per melted column.
✗ Incorrect
Melt stacks each value column per original row, so total rows = original rows * number of melted columns = 3 * 4 = 12.
🔧 Debug
advanced1: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)
Attempts:
2 left
💡 Hint
Check if 'C' exists as a column in df.
✗ Incorrect
The id_vars parameter specifies columns to keep fixed. Since 'C' is not a column, pandas raises KeyError.
🚀 Application
advanced2: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?
Attempts:
2 left
💡 Hint
id_vars should be the column to keep fixed, var_name the new column for old column names.
✗ Incorrect
To reshape questions into one column 'Question' and answers into 'Answer', keep 'Respondent' fixed as id_vars.
🧠 Conceptual
expert2: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?
Attempts:
2 left
💡 Hint
Think about control over column names and identifiers.
✗ Incorrect
Melt lets you choose which columns to keep fixed and rename the resulting columns, which stack does not easily allow.