Exercise · intermediate · 15 min
Unpivot survey answers without inventing scores
A survey export has one column per question. Reshape it to one row per answer, and leave unanswered questions out rather than scoring them zero.
The rules
These rules decide what a correct result is. Your answer is checked on this dataset and on hidden datasets that follow the same rules.
Input survey(respondent, q1, q2, q3) — text. respondent is unique. Each answer is a whole number 1–5 or missing. Rules Reshape to one row per respondent and question: question is the column name (q1, q2 or q3), score is the answer as a whole number. An unanswered question produces no row. It is not a score of 0 and it is not an error. Output (row order does not matter) clean(respondent text, question text, score int64)
The data
survey · 6 rows
| respondent | q1 | q2 | q3 |
|---|---|---|---|
| r344 | 2 | 1 | missing |
| r390 | missing | missing | missing |
| r342 | 5 | 3 | missing |
| r340 | missing | 1 | 2 |
| r341 | 5 | missing | 1 |
| r343 | 3 | 2 | 4 |
What you practise
- unpivot
- missing values
- type conversion
- reshaping
Your answer is also checked on hidden datasets generated from the same rules.
Sign in to bookmark this and keep private notes.