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.
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.
The pandas library offers a built-in method called melt which can directly transform data frames from wide to long format. This approach is particularly useful and efficient for reshaping data.
This solution uses the pandas.melt function to reshape the data from wide to long format. The id_vars parameter is used to identify the columns that should remain steady, while the var_name and value_name specify the names for the new dimension and its corresponding values.
Python
Time Complexity: O(n), where n is the number of elements in the DataFrame because the function iterates through each element.
Space Complexity: O(n) as a new DataFrame is created to store the reshaped data.
Here, we manually loop through the data, restructuring it by iterating over each record. This method provides a more granular approach compared to using built-in functions.
This solution constructs a new DataFrame by manually iterating over each entry in the original data. It collects product names, each quarter, and the associated sales figure. Each item is appended to a list that forms the structure of the new DataFrame.
Python
Time Complexity: O(p * q), where p is the number of products and q is the number of quarters in the data.
Space Complexity: O(n), where n is the total number of elements in the reshaped DataFrame.
Python
| Approach | Complexity |
|---|---|
| Method 1: Using Pandas Melt Function | Time Complexity: O(n), where n is the number of elements in the DataFrame because the function iterates through each element. |
| Method 2: Manual Reshaping with Iteration | Time Complexity: O(p * q), where p is the number of products and q is the number of quarters in the data. |
| Default Approach | — |
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Using Pandas melt() | O(n) | O(n) | Standard pandas workflow for reshaping wide tables into long format |
| Manual Reshaping with Iteration | O(n) | O(n) | When implementing reshaping logic without built-in helpers or for learning how melt works internally |
Reshape Data Melt LeetCode 2890 • CuteLeetCrafter • 316 views views
Watch 2 more video solutions →Practice Reshape Data: Melt with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor