1+ """
2+ 2890. Reshape Data: Melt
3+ Solved
4+ Easy
5+ Companies
6+ Hint
7+ DataFrame report
8+ +-------------+--------+
9+ | Column Name | Type |
10+ +-------------+--------+
11+ | product | object |
12+ | quarter_1 | int |
13+ | quarter_2 | int |
14+ | quarter_3 | int |
15+ | quarter_4 | int |
16+ +-------------+--------+
17+ Write a solution to reshape the data so that each row represents sales data for a product in a specific quarter.
18+
19+ The result format is in the following example.
20+
21+ Example 1:
22+
23+ Input:
24+ +-------------+-----------+-----------+-----------+-----------+
25+ | product | quarter_1 | quarter_2 | quarter_3 | quarter_4 |
26+ +-------------+-----------+-----------+-----------+-----------+
27+ | Umbrella | 417 | 224 | 379 | 611 |
28+ | SleepingBag | 800 | 936 | 93 | 875 |
29+ +-------------+-----------+-----------+-----------+-----------+
30+ Output:
31+ +-------------+-----------+-------+
32+ | product | quarter | sales |
33+ +-------------+-----------+-------+
34+ | Umbrella | quarter_1 | 417 |
35+ | SleepingBag | quarter_1 | 800 |
36+ | Umbrella | quarter_2 | 224 |
37+ | SleepingBag | quarter_2 | 936 |
38+ | Umbrella | quarter_3 | 379 |
39+ | SleepingBag | quarter_3 | 93 |
40+ | Umbrella | quarter_4 | 611 |
41+ | SleepingBag | quarter_4 | 875 |
42+ +-------------+-----------+-------+
43+ Explanation:
44+ The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter."
45+ """
46+
47+ import pandas as pd
48+
49+ def meltTable (report : pd .DataFrame ) -> pd .DataFrame :
50+ return report .melt (id_vars = 'product' , var_name = 'quarter' , value_name = 'sales' )
0 commit comments