Why does Seaborn expect long/tidy input rather than wide form, and how do melt and pivot fit in as a pre-plotting step?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
A dataset stores one row per subject and one column per trial (`trial_1`, `trial_2`, `trial_3`). Why does Seaborn expect long/tidy input instead of wide form, and how do `melt` and `pivot` fit in as a pre-plotting step?
```hint Look at the plotting arguments
Look at what you pass to `x`, `y` and `hue` in a Seaborn call, and ask which column of the wide table you would pass.
```
```hint Not every plot wants the same shape
Compare the input a heatmap needs with the input a bar or line plot needs.
```
### What a Strong Answer Covers
- What long (tidy) and wide forms are, with an example of each.
- Why Seaborn's mapping of columns to roles such as `x`, `hue` and `col` needs one column per variable.
- How `melt` turns wide data into long data, including `id_vars`, `var_name` and `value_name`.
- Why `heatmap` and `clustermap` need the opposite shape, and how `pivot` or `pivot_table` produces it.
- The limited wide-form support Seaborn does have, and when it is enough.
### Follow-up Questions
- Column names such as `control_t1` and `treatment_t2` encode two variables. How would you reshape them for plotting?
- When does `pivot` fail, and what does `pivot_table` do differently?
- After a pivot, the heatmap's columns appear as `trial_1`, `trial_10`, `trial_2`. Why, and how would you fix it?
- If you aggregate the data before calling `barplot`, what happens to the error bars?
Overview: Asks why Seaborn works best with long, tidy data and how melt and pivot reshape a DataFrame before plotting. It tests mapping columns to plot roles such as hue, converting wide tables with melt, and building the matrix that heatmaps need with pivot or pivot_table.