Challenge - 5 Problems
Melt Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of melt() with id_vars and value_vars
What is the output DataFrame after running the following code?
Pandas
import pandas as pd df = pd.DataFrame({ 'City': ['NY', 'LA'], '2019': [100, 200], '2020': [110, 210] }) result = pd.melt(df, id_vars=['City'], value_vars=['2019', '2020'], var_name='Year', value_name='Population') print(result)
Attempts:
2 left
💡 Hint
Remember melt keeps id_vars as is and stacks value_vars into rows.
✗ Incorrect
The melt function keeps 'City' as identifier and unpivots '2019' and '2020' columns into 'Year' and 'Population'. The order is by rows then columns.
❓ data_output
intermediate1:30remaining
Number of rows after melting
Given a DataFrame with 3 rows and 4 columns, if you melt it using 1 column as id_vars and 3 columns as value_vars, how many rows will the melted DataFrame have?
Pandas
import pandas as pd df = pd.DataFrame({ 'ID': [1, 2, 3], 'A': [10, 20, 30], 'B': [40, 50, 60], 'C': [70, 80, 90] }) melted = pd.melt(df, id_vars=['ID'], value_vars=['A', 'B', 'C']) print(len(melted))
Attempts:
2 left
💡 Hint
Each row is repeated for each value_var column.
✗ Incorrect
There are 3 rows and 3 value_vars, so total rows after melt = 3 * 3 = 9.
🔧 Debug
advanced2:00remaining
Identify the error in melt usage
What error will this code raise and why?
Pandas
import pandas as pd df = pd.DataFrame({ 'Name': ['Alice', 'Bob'], 'Math': [90, 80], 'Science': [85, 95] }) result = pd.melt(df, id_vars='Name', value_vars='Math,Science') print(result)
Attempts:
2 left
💡 Hint
Check the type of value_vars argument.
✗ Incorrect
value_vars='Math,Science' is a single string, so pandas looks for a column named exactly 'Math,Science' which does not exist, causing KeyError.
🚀 Application
advanced2:30remaining
Using melt to reshape sales data
You have a DataFrame with columns: 'Store', 'Jan_Sales', 'Feb_Sales', 'Mar_Sales'. You want to reshape it so that each row shows 'Store', 'Month', and 'Sales'. Which melt call achieves this?
Pandas
import pandas as pd df = pd.DataFrame({ 'Store': ['A', 'B'], 'Jan_Sales': [100, 150], 'Feb_Sales': [110, 160], 'Mar_Sales': [120, 170] })
Attempts:
2 left
💡 Hint
Specify id_vars and value_vars explicitly to keep 'Store' and unpivot sales columns.
✗ Incorrect
Option D correctly keeps 'Store' as id_vars and melts the sales columns into 'Month' and 'Sales'. Option D melts all columns including 'Store', causing wrong output.
🧠 Conceptual
expert3:00remaining
Understanding melt with multiple id_vars
Given a DataFrame with columns ['Region', 'Year', 'Q1', 'Q2'], what does melt(df, id_vars=['Region', 'Year'], value_vars=['Q1', 'Q2']) do?
Attempts:
2 left
💡 Hint
melt keeps all id_vars columns as is and stacks value_vars into two columns.
✗ Incorrect
melt keeps 'Region' and 'Year' columns as identifiers and unpivots 'Q1' and 'Q2' into 'variable' and 'value' columns.