Problem

DataFrame report +————-+——–+ | Column Name | Type | +————-+——–+ | product | object | | quarter_1 | int | | quarter_2 | int | | quarter_3 | int | | quarter_4 | int | +————-+——–+

Write a solution to reshape the data so that each row represents sales data for a product in a specific quarter.

The result format is in the following example.

Examples

Example 1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Input: +-------------+-----------+-----------+-----------+-----------+
| product     | quarter_1 | quarter_2 | quarter_3 | quarter_4 |
+-------------+-----------+-----------+-----------+-----------+
| Umbrella    | 417       | 224       | 379       | 611       |
| SleepingBag | 800       | 936       | 93        | 875       |
+-------------+-----------+-----------+-----------+-----------+
Output:
+-------------+-----------+-------+
| product     | quarter   | sales |
+-------------+-----------+-------+
| Umbrella    | quarter_1 | 417   |
| SleepingBag | quarter_1 | 800   |
| Umbrella    | quarter_2 | 224   |
| SleepingBag | quarter_2 | 936   |
| Umbrella    | quarter_3 | 379   |
| SleepingBag | quarter_3 | 93    |
| Umbrella    | quarter_4 | 611   |
| SleepingBag | quarter_4 | 875   |
+-------------+-----------+-------+
Explanation:
The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.

## Solution

### Method 1 - Pandas Melt

#### Intuition
We want to convert the DataFrame from wide to long format, so each row is a product-quarter-sales triple. This is a classic use case for pandas `melt`.

#### Approach
Use `pd.melt` to unpivot the DataFrame, renaming columns as needed.

#### Code

1
2
3
def melt_report(report: pd.DataFrame) -> pd.DataFrame:
    result = report.melt(id_vars=['product'], var_name='quarter', value_name='sales')
    return result
#### Complexity * โฐ Time complexity: `O(n)` โ€” where n is the number of cells in the DataFrame. * ๐Ÿงบ Space complexity: `O(n)` โ€” for the output DataFrame.