Reshape a wide yearly sales table to long format in pandas
Quick Overview
Reshape a wide pandas DataFrame with one sales column per year into long format with country, year and sales columns. It tests the arguments of melt, the row order and index of the reshaped frame, and the type of the new year column.
Reshape a wide yearly sales table to long format in pandas
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
Implement `to_long(df: pd.DataFrame) -> pd.DataFrame`. The input is a wide frame with columns `['country', '2020', '2021', '2022']` (string `country`, integer sales) and a default `RangeIndex`. Reshape it to long form with `melt`, keeping `country` as the identifier.
Requirements:
- The columns are exactly `['country', 'year', 'sales']` (`var_name='year'`, `value_name='sales'`).
- The result has a fresh default `RangeIndex` (`ignore_index=True`).
- Row order: all `'2020'` rows, then `'2021'`, then `'2022'`.
```text
# input (wide)
country 2020 2021 2022
0 US 100 110 120
1 UK 90 95 100
# output (long)
country year sales
0 US 2020 100
1 UK 2020 90
2 US 2021 110
3 UK 2021 95
4 US 2022 120
5 UK 2022 100
```
```hint Follow the column labels
After the reshape, the old column labels become values in a single column. Check the order they come out in and what type they have.
```
### What a Strong Answer Covers
- Produces exactly the three required columns, in the required order, with the right names.
- Explains why the rows come out grouped by year in the stated order and why the index is a fresh `RangeIndex`.
- Notes the type of the `year` values and what downstream code may need from it.
- Considers what happens when the frame gains extra columns or has missing sales values.
### Follow-up Questions
- The `year` column holds strings like `'2020'`. How would you make it integer?
- How would you return the rows sorted by country and then year instead?
- How would you reverse the reshape and rebuild the wide frame from the long one?
Overview: Reshape a wide pandas DataFrame with one sales column per year into long format with country, year and sales columns. It tests the arguments of melt, the row order and index of the reshaped frame, and the type of the new year column.
Implement to_long(df: pd.DataFrame) -> pd.DataFrame. The input is a wide frame with columns ['country', '2020', '2021', '2022'] (string country, integer sales) and a default RangeIndex. Reshape it to long form with melt, keeping country as the identifier.
Requirements:
The columns are exactly
['country', 'year', 'sales']
(
var_name='year'
,
value_name='sales'
).
The result has a fresh default
RangeIndex
(
ignore_index=True
).
Row order: all
'2020'
rows, then
'2021'
, then
'2022'
.
# input (wide)
country 2020 2021 2022
0 US 100 110 120
1 UK 90 95 100
# output (long)
country year sales
0 US 2020 100
1 UK 2020 90
2 US 2021 110
3 UK 2021 95
4 US 2022 120
5 UK 2022 100
What a Strong Answer Covers Guidance
Produces exactly the three required columns, in the required order, with the right names.
Explains why the rows come out grouped by year in the stated order and why the index is a fresh
RangeIndex
.
Notes the type of the
year
values and what downstream code may need from it.
Considers what happens when the frame gains extra columns or has missing sales values.
Follow-up Questions Guidance
The
year
column holds strings like
'2020'
. How would you make it integer?
How would you return the rows sorted by country and then year instead?
How would you reverse the reshape and rebuild the wide frame from the long one?