Reshape Data: Melt - Problem
DataFrame Transformation Challenge: Reshape Data with Melt Operation

You are working as a data analyst for a company that tracks quarterly sales data. The data is currently stored in a wide format where each product has its quarterly sales data spread across multiple columns (quarter_1, quarter_2, quarter_3, quarter_4).

Your task is to reshape this data into a long format where each row represents a single product-quarter combination. This transformation is commonly known as a "melt" operation in data science.

Input Format:
A DataFrame with columns: product (string), quarter_1 (int), quarter_2 (int), quarter_3 (int), quarter_4 (int)

Output Format:
A reshaped DataFrame with columns: product (string), quarter (string), sales (int)

Goal: Transform wide data format to long data format, making it easier for analysis and visualization.

Input & Output

example_1.py โ€” Basic Melt Operation
$ Input: report = pd.DataFrame({ 'product': ['Laptop', 'Mouse'], 'quarter_1': [1000, 500], 'quarter_2': [1100, 550], 'quarter_3': [950, 600], 'quarter_4': [1200, 450] })
โ€บ Output: product quarter sales 0 Laptop quarter_1 1000 1 Laptop quarter_2 1100 2 Laptop quarter_3 950 3 Laptop quarter_4 1200 4 Mouse quarter_1 500 5 Mouse quarter_2 550 6 Mouse quarter_3 600 7 Mouse quarter_4 450
๐Ÿ’ก Note: Each product row is transformed into 4 rows, one for each quarter. The wide format with quarter columns becomes a long format with quarter and sales columns.
example_2.py โ€” Single Product
$ Input: report = pd.DataFrame({ 'product': ['Keyboard'], 'quarter_1': [300], 'quarter_2': [320], 'quarter_3': [290], 'quarter_4': [350] })
โ€บ Output: product quarter sales 0 Keyboard quarter_1 300 1 Keyboard quarter_2 320 2 Keyboard quarter_3 290 3 Keyboard quarter_4 350
๐Ÿ’ก Note: Even with a single product, the melt operation creates 4 rows, transforming the wide format into the expected long format structure.
example_3.py โ€” Multiple Products
$ Input: report = pd.DataFrame({ 'product': ['Phone', 'Tablet', 'Watch'], 'quarter_1': [2000, 1500, 800], 'quarter_2': [2200, 1600, 900], 'quarter_3': [1900, 1400, 750], 'quarter_4': [2400, 1800, 950] })
โ€บ Output: product quarter sales 0 Phone quarter_1 2000 1 Phone quarter_2 2200 2 Phone quarter_3 1900 3 Phone quarter_4 2400 4 Tablet quarter_1 1500 5 Tablet quarter_2 1600 6 Tablet quarter_3 1400 7 Tablet quarter_4 1800 8 Watch quarter_1 800 9 Watch quarter_2 900 10 Watch quarter_3 750 11 Watch quarter_4 950
๐Ÿ’ก Note: With 3 products, the result contains 12 rows (3 ร— 4 quarters). Each product's quarterly data is properly melted into separate rows while maintaining the product identifier.

Visualization

Tap to expand
๐Ÿ“‹ Wide Format (Before)Product | Q1 | Q2 | Q3 | Q4Laptop | 1000 | 1100 | 950 | 1200Mouse | 500 | 550 | 600 | 450Phone | 2000 | 2200 | 1900 | 2400๐Ÿ”„ MELTTransformColumns โ†’ Rows๐Ÿ“ˆ Long Format (After)Product | Quarter | SalesLaptop | quarter_1 | 1000Laptop | quarter_2 | 1100Laptop | quarter_3 | 950Laptop | quarter_4 | 1200Mouse | quarter_1 | 500Mouse | quarter_2 | 550Mouse | quarter_3 | 600Mouse | quarter_4 | 450Phone | quarter_1 | 2000... (4 more rows)๐Ÿ’ก Key Benefitsโ€ข Better for analysis and visualizationโ€ข Database-friendly format
Understanding the Visualization
1
๐Ÿ“‹ Wide Format Analysis
Original data has products as rows and quarters as separate columns - like a spreadsheet
2
๐Ÿ”„ Identification Phase
Identify which columns to keep (product) and which to melt (all quarter columns)
3
โšก Melt Transformation
Built-in melt function efficiently transforms column headers into row values
4
๐Ÿ“ˆ Long Format Result
Each original row becomes multiple rows - one for each quarter with proper labeling
Key Takeaway
๐ŸŽฏ Key Insight: Melt operations transform wide data (multiple columns) into long data (multiple rows), making it ideal for analysis, visualization, and database storage. The built-in functions handle this efficiently with optimized algorithms.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— m)

Where n is number of rows and m is number of columns to melt, optimized internally

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— m)

Same output size as manual approach but with better memory management

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค number of products โ‰ค 1000
  • 0 โ‰ค quarterly sales values โ‰ค 106
  • Product names are non-empty strings with length โ‰ค 50
  • Each product must have exactly 4 quarters of data
  • Output should maintain the order: all quarters for first product, then all quarters for second product, etc.
Asked in
Netflix 35 Spotify 28 Airbnb 22 Uber 18
32.0K Views
High Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen