Data Cleaning Exercises

Reference · Python (pandas)

Unpivot wide data to long in Python (pandas)

Turn several columns into key/value rows, without inventing rows for missing values.

When to use it

Survey or spreadsheet exports with one column per question or period.

How

DataFrame.melt, then drop rows with a missing value.

long = survey.melt(id_vars=["respondent"], var_name="question", value_name="score")
long = long[long["score"].notna()]
  • melt keeps missing values, unlike DuckDB's default UNPIVOT.

Pitfalls

  • Scoring missing answers as 0.
  • Mixing types in the unpivoted columns.

Lesson: Unpivot without inventing scores

Practise it: Unpivot survey answers without inventing scores