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 (
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:
Output Format:
A reshaped DataFrame with columns:
Goal: Transform wide data format to long data format, making it easier for analysis and visualization.
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
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
โ Linear Growth
Space Complexity
O(n ร m)
Same output size as manual approach but with better memory management
โก 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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code