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.
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.
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.
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.
| 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. |
Python Pandas Tutorial 11. Reshape dataframe using melt • codebasics • 128,327 views views
Watch 9 more video solutions →Practice Reshape Data: Melt with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor