Problem Description
Given a DataFrame report
containing sales data for various products across four quarters, write a solution to reshape the data so that each row represents sales data for a product in a specific quarter.
Key Insights
- The input DataFrame is in a wide format, where sales figures for each quarter are in separate columns.
- The output should be in a long format, where each product has multiple rows corresponding to each quarter.
- We need to transform the quarter columns into a single column while retaining the product names and their respective sales.
Space and Time Complexity
Time Complexity: O(n) - where n is the number of products, as we are iterating through each product and each quarter. Space Complexity: O(n) - for storing the reshaped data in the new format.
Solution
To solve this problem, we will use a data transformation technique to convert the DataFrame from a wide format to a long format. Specifically, we will:
- Identify the columns that need to be reshaped (the quarter columns).
- Use a method to "melt" the DataFrame, which will consolidate the quarter columns into a single column while keeping the product names intact.
- The resulting DataFrame will consist of three columns:
product
,quarter
, andsales
.