Reshape Data: Pivot - Problem
Transform Your Data with Pivot Operations
You're given a DataFrame containing weather data with city, month, and temperature information. Your task is to reshape this data by pivoting it so that:
• Each row represents a specific month
• Each column represents a different city
• The values show the temperature for that city in that month
This is a common data transformation operation used in analytics and reporting. Think of it as rotating your data table to get a different perspective on the same information.
You're given a DataFrame containing weather data with city, month, and temperature information. Your task is to reshape this data by pivoting it so that:
• Each row represents a specific month
• Each column represents a different city
• The values show the temperature for that city in that month
This is a common data transformation operation used in analytics and reporting. Think of it as rotating your data table to get a different perspective on the same information.
Input DataFrame:city | month | temperature
---------|-------|------------
London | Jan | 5
London | Feb | 8
Paris | Jan | 7
Paris | Feb | 10
Output (Pivoted):month | London | Paris
------|--------|------
Jan | 5 | 7
Feb | 8 | 10
Input & Output
basic_weather.py — Basic Weather Data
$
Input:
weather = pd.DataFrame({
'city': ['London', 'London', 'Paris', 'Paris'],
'month': ['Jan', 'Feb', 'Jan', 'Feb'],
'temperature': [5, 8, 7, 10]
})
›
Output:
month London Paris
0 Jan 5 7
1 Feb 8 10
💡 Note:
Simple pivot operation transforms long-form weather data into a month-by-city comparison table. Each city becomes a column, each month becomes a row.
multiple_cities.py — Multiple Cities Dataset
$
Input:
weather = pd.DataFrame({
'city': ['London', 'Paris', 'Berlin', 'London', 'Paris', 'Berlin'],
'month': ['Jan', 'Jan', 'Jan', 'Feb', 'Feb', 'Feb'],
'temperature': [5, 7, 2, 8, 10, 6]
})
›
Output:
month Berlin London Paris
0 Jan 2 5 7
1 Feb 6 8 10
💡 Note:
With more cities, the pivot creates additional columns. Cities are automatically sorted alphabetically, and the table shows temperature comparison across all cities for each month.
missing_data.py — Handling Missing Values
$
Input:
weather = pd.DataFrame({
'city': ['London', 'Paris', 'London'],
'month': ['Jan', 'Jan', 'Feb'],
'temperature': [5, 7, 8]
})
›
Output:
month London Paris
0 Jan 5 7
1 Feb 8 NaN
💡 Note:
When data is missing for certain month-city combinations, pandas automatically fills those cells with NaN (Not a Number) values, preserving the table structure.
Visualization
Tap to expand
Understanding the Visualization
1
Original Layout
Cards are arranged in a long list format - one card per city-month combination
2
Identify Structure
Recognize that months should become row headers and cities should become column headers
3
Reorganize Cards
Physically move cards into a grid where month-city intersections show temperatures
4
Final Grid
Result is a structured comparison table perfect for analysis
Key Takeaway
🎯 Key Insight: Pivot operations transform data perspective from record-by-record to dimension-by-dimension analysis, making patterns and comparisons immediately visible.
Time & Space Complexity
Time Complexity
O(n)
Single pass through data with efficient grouping operations
✓ Linear Growth
Space Complexity
O(m×c)
Output size determined by unique months × unique cities
✓ Linear Space
Constraints
- 1 ≤ number of records ≤ 104
- City and month names are non-empty strings
- Temperature values are integers between -50 and 50
- Each city-month combination appears at most once
- At least one complete record must be present
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code