Data Cleaning Exercises

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

respondentq1q2q3
r34421missing
r390missingmissingmissing
r34253missing
r340missing12
r3415missing1
r343324

What you practise

  • unpivot
  • missing values
  • type conversion
  • reshaping

Your answer is also checked on hidden datasets generated from the same rules.

Download this dataset (CSV) with its data dictionary

Sign in to bookmark this and keep private notes.