Skip to main content

Reshape Data: Melt - Video Solutions

Easy

Reshape Data Melt LeetCode 2890

CuteLeetCrafter
1:53324 views
4 video solutions available

Reshape Data: Melt - Video Solution

Watch 4 video solutions for Reshape Data: Melt, a easy level problem. This walkthrough by CuteLeetCrafter has 324 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.

Problem Statement

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.

 

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   |
+-------------+-----------+-------+
Explanation:
The DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.
Read full problem with examples

Approach Overview

Problem Overview: You receive a pandas DataFrame in wide format where multiple columns represent similar values (for example quarterly sales). The task is to reshape the table into a long format with three columns: the identifier column, a variable column (the original column names), and a value column.

Approach 1: Using Pandas Melt Function (O(n) time, O(n) space)

The most direct solution uses pandas.melt(), which is designed specifically for converting wide tables into long format. You specify the identifier column with id_vars, the columns to unpivot with value_vars, and provide names for the resulting variable and value columns. Internally, pandas iterates through each selected column and stacks the values into a new DataFrame. The time complexity is O(n), where n is the total number of cells being reshaped, and the space complexity is also O(n) because a new DataFrame is created. This approach is concise, readable, and the standard solution when working with pandas or DataFrame manipulation.

Approach 2: Manual Reshaping with Iteration (O(n) time, O(n) space)

If you want to understand what melt() does internally, you can manually reshape the data. Iterate through every row of the DataFrame and then iterate through each column that needs to be unpivoted. For each column, create a new record containing the identifier value, the column name (as the variable), and the corresponding cell value. Append these records to a list and convert the list into a new DataFrame. This approach also runs in O(n) time because each cell is processed exactly once, with O(n) additional space to store the reshaped rows. It’s useful when implementing similar transformations without relying on built-in helpers or when learning how data reshaping operations work internally.

Recommended for interviews: The expected solution is the pandas.melt() approach because it demonstrates familiarity with standard pandas transformations and keeps the implementation clean. Showing the manual iteration approach can still be valuable—it proves you understand how the reshape operation works under the hood—but production code and interview solutions typically favor the built‑in melt function.

Complexity Analysis

ApproachTimeSpaceWhen to Use
Using Pandas melt()O(n)O(n)Standard pandas workflow for reshaping wide tables into long format
Manual Reshaping with IterationO(n)O(n)When implementing reshaping logic without built-in helpers or for learning how melt works internally