Challenge - 5 Problems
Wide to Long Format Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pandas melt function
What is the output DataFrame after running this code?
Pandas
import pandas as pd df = pd.DataFrame({ 'id': [1, 2], 'A_2020': [10, 20], 'A_2021': [15, 25], 'B_2020': [5, 10], 'B_2021': [7, 14] }) result = df.melt(id_vars=['id'], var_name='variable', value_name='value') print(result)
Attempts:
2 left
💡 Hint
The melt function keeps id_vars as is and stacks other columns into two columns: variable and value.
✗ Incorrect
The melt function converts wide columns into rows. Here, columns except 'id' become values in 'variable' column, and their data go into 'value'.
❓ data_output
intermediate1:30remaining
Number of rows after wide to long conversion
Given a DataFrame with 3 rows and 4 value columns (excluding id), how many rows will the DataFrame have after using pandas melt with id_vars=['id']?
Attempts:
2 left
💡 Hint
Each original row expands into multiple rows, one per value column melted.
✗ Incorrect
Melt stacks all value columns into rows. With 3 rows and 4 value columns, total rows = 3 * 4 = 12.
🔧 Debug
advanced2:00remaining
Identify the error in this wide to long conversion code
What error does this code raise when run?
Pandas
import pandas as pd df = pd.DataFrame({ 'id': [1, 2], 'score_1': [10, 20], 'score_2': [15, 25] }) result = pd.melt(df, id_vars='id', var_name='test', value_name='score') print(result)
Attempts:
2 left
💡 Hint
Check the type of id_vars argument in melt function.
✗ Incorrect
id_vars expects a list of column names, but a string was passed. This causes a TypeError.
🚀 Application
advanced2:30remaining
Extract year and variable from melted column
After melting a DataFrame with columns like 'A_2020', 'B_2021', how can you split the 'variable' column into two new columns 'var' and 'year'?
Pandas
import pandas as pd df = pd.DataFrame({ 'id': [1, 2], 'A_2020': [10, 20], 'B_2021': [5, 15] }) melted = df.melt(id_vars=['id'], var_name='variable', value_name='value') # Fill in the code to split 'variable' into 'var' and 'year'
Attempts:
2 left
💡 Hint
Use pandas string split with expand=True to create multiple columns.
✗ Incorrect
Using str.split('_', expand=True) returns a DataFrame with two columns which can be assigned to new columns.
🧠 Conceptual
expert1:30remaining
Why use wide to long format in data analysis?
Which of these is the best reason to convert data from wide to long format?
Attempts:
2 left
💡 Hint
Think about how data shape affects grouping and summarizing.
✗ Incorrect
Long format stacks variables into rows, making it easier to group and aggregate by variable types or time points.