Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f1eae45

Browse files
Add file
1 parent 41898b2 commit f1eae45

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /