Skip to content

Practice Reshape Data: Melt

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2890
LeetCode Problem

Reshape Data: Melt

Problem Statement

The report DataFrame has product (object) and quarter_1, quarter_2, quarter_3 and quarter_4 (all int) columns. Reshape it into product, quarter and sales columns, with one row for each original product and quarter. quarter contains the original quarter-column name, and sales contains its value.

Source: Reshape Data: Melt on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

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   | +-------------+-----------+-------+

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.