Melt Wide Ad Recall Survey Responses into Tidy Long Format
Last updated: November 29, 2026
Quick Overview
A brand lift survey export has one row per respondent with columns q1_score through q5_score plus a test or control group label. Reshape it to long format and compute, for each question, the mean score and response count per group, ignoring skipped answers.
Meta
November 29, 20260
0
0 solved
A brand lift survey export has one row per respondent with columns q1_score through q5_score plus a test or control group label. Reshape it to long format and compute, for each question, the mean score and response count per group, ignoring skipped answers.
Meta's brand lift studies compare survey answers between exposed and holdout groups, and exports are often wide. The interviewer wants to see melt used correctly, question ids parsed from column names, and skipped answers handled as missing rather than zero.
What the Interviewer Expects
- Use melt with id_vars and a value_name
- Extract the question number from column names with a regex
- Drop NaN responses so skips do not lower the mean
- Aggregate mean and count per question and group
- Pivot the group back to columns for side-by-side comparison
Key Topics to Cover
How to Approach This
- Clarify the schema and expected output format before writing queries.
- Use CTEs (WITH clauses) to break complex queries into readable steps.
- Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
- Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
- For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
- How would you compute the lift and its standard error for each question?
- What if some respondents saw only a subset of questions by design?
- How would wide_to_long help if there were also q1_time columns?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice SQL ProblemsSample Answer
Clarify the Data
Columns respondent_id, group ('test' or 'control'), q1_score to q5_score on a 1 to 5 scale, NaN when skipped. Output: one row per question with test_mean, control_mean, test_n, `control_...
Approach
Melt the score columns into question and score, extract the integer from q(\d+)_score, drop NaN scores, then aggregate mean and count by question and group. Unstack the group level to get test a...