Back to Practice
#0082
Melt Wide Score Columns
MediumPandas18 min20 XP
Problem
Given a DataFrame with user_id, math_score, and sql_score, return a long DataFrame with columns user_id, subject, score. subject should be math or sql.
Why This Matters
Many datasets arrive wide, but plotting and grouped analysis often work better in long format.
Function Signature
def melt_scores(df):
Examples
Example 1
Inputone row with math_score and sql_score
Outputtwo rows
Each score column becomes a subject row.
Constraints
- Return columns user_id, subject, score.
- Remove _score from subject names.
- Reset the index.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Two users two subjects
Inputwide scores
Expectedfour long rows
Each user has one row per subject.
Hidden Test Categories
single userindex resetsubject name cleanup