2890. Reshape Data Melt

Problem Description

The given LeetCode problem presents a DataFrame named report which contains sales data for different products across four quarters (quarter_1, quarter_2, quarter_3, quarter_4). Each row of this DataFrame represents a product and the sales figures for each of the four quarters are in separate columns. The task is to reshape this data such that the resulting DataFrame has a row for each product and quarter combination. Essentially, it involves converting the wide format of the DataFrame (where quarters are spread across columns) into a long format (where quarter data is stacked into single column with corresponding sales figures).

The expected output is a DataFrame with three columns: 'product', 'quarter', and 'sales'. Each row should contain a product name, a specific quarter, and the sales for that product in that quarter.


The intuition behind the solution is that we want to "melt" the wide DataFrame into a long DataFrame. In pandas, the melt function is used for just such a transformation. It takes the following parameters:

  • id_vars: The column(s) of the old DataFrame to preserve as identifier variables. In this case, it's the 'product' column, as we want to keep that fixed for each entry.
  • var_name: The name to give the variable column that will hold the names of the former columns. We will name it 'quarter' since it will contain the names of the quarter columns.
  • value_name: The name to give the value column that will contain the values from the former quarter columns. We'll call this 'sales' to indicate that these values represent the sales amounts.

By applying the melt method to the report dataframe, it will take each entry from the quarter-specific columns and place it into its own row, associated with the appropriate product and tagged with the corresponding quarter, achieving the desired reshaping of the data.

Solution Approach

The solution makes use of the melt function from the pandas library. This function is designed to transform a DataFrame from a wide format to a long format, which is exactly what is required in the problem. The melt function can be seen as a way to 'unpivot' or 'reshape' the data.

Here's a step-by-step walkthrough of the meltTable function shown in the reference solution:

  • We start by passing the report DataFrame to the meltTable function.
  • Within the function, we call pd.melt on the report DataFrame.

The pd.melt function is called with the following arguments:

  • id_vars=['product']: This specifies that the 'product' column should stay as is and not be unpivoted. This column is used as the identifier variable.
  • var_name='quarter': This argument tells pandas to name the new column that holds the 'variables', which were originally the column names (quarter_1, quarter_2, quarter_3, quarter_4), as 'quarter'.
  • value_name='sales': This specifies that the new column that holds the values from the variable columns should be named 'sales'.

The melt function processes the DataFrame report by keeping the 'product' column fixed and ‘melting’ the quarter columns. For each product, it creates a new row for each quarter column, filling in the 'quarter' column with the quarter column name (e.g., 'quarter_1') and the 'sales' column with the corresponding sales value.

As a result, what was previously structured as one row per product with multiple columns for each quarter becomes multiple rows for each product, with each row representing a different quarter.

By using pandas and its melt function, the solution effectively harnesses the power of an established data manipulation tool to accomplish the task in a concise and efficient manner without the need for writing complex data reshaping code from scratch.

Discover Your Strengths and Weaknesses: Take Our 2-Minute Quiz to Tailor Your Study Plan:

What is the best way of checking if an element exists in an unsorted array once in terms of time complexity? Select the best that applies.

Example Walkthrough

Let's say we have the following small 'report' DataFrame as an example:


We want to reshape this data to create a 'long' format DataFrame, where each product and quarter combination gets its own row. Here's how we apply the solution approach:

  1. We pass this 'report' DataFrame to our meltTable function.
  2. Inside meltTable, we use pd.melt and specify three key parameters:
    • id_vars=['product'] ensures that the 'product' column is preserved in the transformation.
    • var_name='quarter' creates a new column named 'quarter', which will contain the names of the original columns that represented each quarter.
    • value_name='sales' specifies that the values from those quarter columns should be placed in a new column called 'sales'.

After calling pd.melt with these parameters, we get the following DataFrame:


The resulting DataFrame has the three columns: 'product', 'quarter', and 'sales', with each row containing a specific combination of product and quarter with the corresponding sales figure. This transformation enables a more detailed analysis of sales data by quarter for each product.

Solution Implementation

1import pandas as pd  # Import the pandas library with alias 'pd'
3def melt_table(report: pd.DataFrame) -> pd.DataFrame:
4    # Function to transform the input DataFrame into a format where each row
5    # represents a single observation for a specific quarter and product.
7    # 'pd.melt': Convert the given DataFrame from wide format to long format.
8    # 'id_vars': Column(s) to use as identifier variables.
9    # 'var_name': Name of the new column created after melting that will hold the variable names.
10    # 'value_name': Name of the new column created that will contain the values.
11    melted_report = pd.melt(report, id_vars=['product'], var_name='quarter', value_name='sales')
12    return melted_report  # Return the melted DataFrame
14# Usage example (not part of the required code rewrite, for illustration purposes):
15# Assuming 'report' is a DataFrame structured with 'product' as one of the columns
16# and other columns represent sales data for each quarter.
17# Example structure of 'report' before melting:
18# product  Q1  Q2  Q3  Q4
19# A        10  15  20  25
20# B        5   10  15  20
21# After calling melt_table(report), the result will be:
22# product quarter sales
23# A       Q1      10
24# B       Q1      5
25# A       Q2      15
26# B       Q2      10
27# ...and so on for each product and quarter.
1import java.util.ArrayList;
2import java.util.HashMap;
3import java.util.List;
4import java.util.Map;
6class SalesReport {
8    // A method to transform a report into a melted format where each row represents a single observation
9    public static List<Map<String, String>> meltTable(List<Map<String, String>> report) {
10        List<Map<String, String>> meltedReport = new ArrayList<>();
12        // Loop over each row (each map is a row with the product and sales data)
13        for (Map<String, String> row : report) {
14            String product = row.get("product");
15            // Loop over each entry in the map, which represents the columns in the original table
16            for (Map.Entry<String, String> entry : row.entrySet()) {
17                if (!entry.getKey().equals("product")) { // Ignore the product column for melting
18                    // Create a new map for the melted row
19                    Map<String, String> meltedRow = new HashMap<>();
20                    meltedRow.put("product", product);
21                    meltedRow.put("quarter", entry.getKey()); // The column name becomes the quarter
22                    meltedRow.put("sales", entry.getValue()); // The value remains the sale amount
23                    meltedReport.add(meltedRow);
24                }
25            }
26        }
27        return meltedReport; // Return the melted table
28    }
30    // Example usage
31    public static void main(String[] args) {
32        List<Map<String, String>> report = new ArrayList<>();
33        Map<String, String> row1 = new HashMap<>();
34        row1.put("product", "A");
35        row1.put("Q1", "10");
36        row1.put("Q2", "15");
37        row1.put("Q3", "20");
38        row1.put("Q4", "25");
39        report.add(row1);
41        Map<String, String> row2 = new HashMap<>();
42        row2.put("product", "B");
43        row2.put("Q1", "5");
44        row2.put("Q2", "10");
45        row2.put("Q3", "15");
46        row2.put("Q4", "20");
47        report.add(row2);
49        List<Map<String, String>> meltedReport = meltTable(report);
51        // Print melted report
52        for (Map<String, String> meltedRow : meltedReport) {
53            System.out.println(meltedRow);
54        }
55    }
1#include <pandas/pandas.h> // Include the pandas C++ library (note: a C++ pandas-like library doesn't exist, but assuming it for the purpose of the exercise.)
3class ReportTransformer {
5    // Transforms the input DataFrame into a format where each row represents a single observation for a specific quarter and product.
6    pandas::DataFrame meltTable(const pandas::DataFrame& report) const {
7        // 'melt': Convert the given DataFrame from wide format to long format.
8        // 'idVars': Vector of column names to use as identifier variables.
9        // 'varName': Name of the new column created after melting that will hold the variable names.
10        // 'valueName': Name of the new column created that will contain the values.
11        pandas::DataFrame meltedReport = report.melt(
12            {"product"}, // idVars
13            "quarter",   // varName
14            "sales"      // valueName
15        );
17        return meltedReport; // Return the melted DataFrame
18    }
21// Usage example:
22// Assuming 'report' is a pandas::DataFrame structured with 'product' as one of the columns
23// and other columns represent sales data for each quarter like Q1, Q2, Q3, Q4.
24// Example structure of 'report' before melting:
25// product  Q1  Q2  Q3  Q4
26// A        10  15  20  25
27// B        5   10  15  20
28// After calling meltTable(report), the result will be:
29// product quarter sales
30// A       Q1      10
31// B       Q1      5
32// A       Q2      15
33// B       Q2      10
34// ...and so on for each product and quarter.
1interface ProductReport {
2    product: string;
3    [key: string]: string | number; // Represents sales data for each quarter with dynamic keys
6interface MeltedReport {
7    product: string;
8    quarter: string;
9    sales: number;
12function meltTable(report: ProductReport[]): MeltedReport[] {
13    // Function to transform the input array of objects into a format
14    // where each entry represents a single observation for a specific quarter and product.
16    let meltedReport: MeltedReport[] = [];
18    // Loop over each product report
19    report.forEach((productReport) => {
20        // Loop over each property in the product report object
21        for (const [key, value] of Object.entries(productReport)) {
22            // Skip the 'product' key as it's the identifier
23            if (key !== 'product') {
24                // Create an object for each quarter with sales data and push it into the meltedReport array
25                meltedReport.push({
26                    product: productReport.product,
27                    quarter: key,
28                    sales: value as number // Assuming the value is always a number for sales data
29                });
30            }
31        }
32    });
34    return meltedReport; // Return the transformed data
37// Usage example:
38// Assuming 'report' is an array of objects structured with 'product' as one of the properties
39// and other properties represent sales data for each quarter.
40// Example structure of 'report' before melting:
41// [
42//   { product: 'A', Q1: 10, Q2: 15, Q3: 20, Q4: 25 },
43//   { product: 'B', Q1: 5, Q2: 10, Q3: 15, Q4: 20 }
44// ]
45// After calling meltTable(report), the result will be:
46// [
47//   { product: 'A', quarter: 'Q1', sales: 10 },
48//   { product: 'A', quarter: 'Q2', sales: 15 },
49//   ...
50//   { product: 'B', quarter: 'Q1', sales: 5 },
51//   { product: 'B', quarter: 'Q2', sales: 10 },
52//   ...
53// ]

Time and Space Complexity

Time Complexity

The meltTable function involves the pd.melt operation from pandas. The time complexity of this operation depends on the size of the input DataFrame. If we assume the input DataFrame has m rows (excluding the header) and n columns (including the 'product' column), then the pd.melt function would iterate through all (m * (n - 1)) elements once, converting them into (m * (n - 1)) rows of the melted DataFrame. Thus, the time complexity is O(m * (n - 1)), which simplifies to O(m * n).

Space Complexity

Regarding the space complexity, pd.melt generates a new DataFrame that has (m * (n - 1)) rows and 3 columns (['product', 'quarter', 'sales']). Hence, the space required for the new DataFrame is proportional to the number of elements in this new structure, which gives us the space complexity of O(m * (n - 1) * 3). Since we tend to ignore constant factors in Big O notation, this simplifies to O(m * n).

Fast Track Your Learning with Our Quick Skills Quiz:

How would you design a stack which has a function min that returns the minimum element in the stack, in addition to push and pop? All push, pop, min should have running time O(1).

Recommended Readings

Got a question? Ask the Monster Assistant anything you don't understand.

Still not clear? Ask in the Forum,  Discord or Submit the part you don't understand to our editors.

Tired of the LeetCode Grind?

Our structured approach teaches you the patterns behind problems, so you can confidently solve any challenge. Get started now to land your dream tech job.

Get Started