What is tidy (long) vs wide data and when should you reshape between them?
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
What is tidy (long) vs wide data, and when should you reshape between them? Use pandas for any code you show.
```hint Add one more month
Picture a table of monthly sales per store. In each layout, what changes when next month's numbers arrive, and what does a groupby or a plotting call expect its input to look like?
```
### What a Strong Answer Covers
- Precise definitions of the long (tidy) and wide layouts, with a small example of the same data in both
- Which tasks each layout suits, including aggregation, plotting, presentation and model input
- The pandas operations for converting in each direction and how they differ
- What can go wrong when reshaping, such as duplicate keys, silent aggregation or lost rows
### Follow-up Questions
- `pivot()` raises "Index contains duplicate entries, cannot reshape". What does that tell you about the data, and how do you fix it?
- Column names look like `sales_2023`, `sales_2024`, `units_2023`, `units_2024`. How do you get to one row per store and year?
- Is a `date, metric, value` table always tidy? When would you not want revenue and sessions in the same value column?
- When does a wide table waste memory compared with a long one?
Overview: Asks what separates tidy (long) data from wide data and when to reshape a table from one layout to the other. Tests the definitions, which layout suits aggregation, plotting and modeling, and the pandas reshape operations and their pitfalls.