We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Reshape Data: Melt

Difficulty: Easy


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:

  1. Identify the columns that need to be reshaped (the quarter columns).
  2. Use a method to "melt" the DataFrame, which will consolidate the quarter columns into a single column while keeping the product names intact.
  3. The resulting DataFrame will consist of three columns: product, quarter, and sales.

Code Solutions

import pandas as pd

# Sample input DataFrame
data = {
    'product': ['Umbrella', 'SleepingBag'],
    'quarter_1': [417, 800],
    'quarter_2': [224, 936],
    'quarter_3': [379, 93],
    'quarter_4': [611, 875]
}

report = pd.DataFrame(data)

# Reshaping the DataFrame
result = pd.melt(report, id_vars=['product'], 
                 value_vars=['quarter_1', 'quarter_2', 'quarter_3', 'quarter_4'],
                 var_name='quarter', value_name='sales')

# Display the result
print(result)
← Back to All Questions