Reshape Data: Pivot - Problem

You have a DataFrame weather with the following structure:

Column NameType
cityobject
monthobject
temperatureint

Write a solution to pivot the data so that each row represents temperatures for a specific month, and each city is a separate column.

The result should transform data from a long format (multiple rows per month) to a wide format (one row per month with cities as columns).

Input & Output

Example 1 — Basic Weather Data
$ Input: weather_data = [["New York", "January", 25], ["Los Angeles", "January", 75], ["New York", "February", 30], ["Los Angeles", "February", 80]]
Output: [["month", "Los Angeles", "New York"], ["February", 80, 30], ["January", 75, 25]]
💡 Note: Transform from long format (city-month-temp rows) to wide format where months become rows and cities become columns. Cities are sorted alphabetically.
Example 2 — Missing Data
$ Input: weather_data = [["Miami", "March", 85], ["Seattle", "April", 55], ["Miami", "April", 88]]
Output: [["month", "Miami", "Seattle"], ["April", 88, 55], ["March", 85, null]]
💡 Note: When a city has no temperature data for a specific month, the pivot table shows null for that cell (Seattle has no March data).
Example 3 — Single Record
$ Input: weather_data = [["Chicago", "December", 15]]
Output: [["month", "Chicago"], ["December", 15]]
💡 Note: Even with minimal data, the pivot structure is maintained with proper headers and single data row.

Constraints

  • 1 ≤ weather_data.length ≤ 1000
  • weather_data[i].length = 3
  • weather_data[i] = [city, month, temperature]
  • All city and month names are non-empty strings
  • All temperatures are integers

Visualization

Tap to expand
Reshape Data: Pivot INPUT (Long Format) city month temp New York January 25 Los Angeles January 75 New York February 30 Los Angeles February 80 4 rows x 3 columns Long Format NY LA ... Jan Jan Feb ALGORITHM STEPS 1 Identify Pivot Parameters index=month, columns=city values=temperature 2 Get Unique Values months: [January, February] cities: [LA, NY] 3 Create Wide Structure Rows = unique months Cols = unique cities 4 Fill Values Map temp to [month,city] pivot_table() 4x3 2x3 FINAL RESULT (Wide) month LA NY February 80 30 January 75 25 2 rows x 3 columns OK - Pivot Complete Wide Format Feb 80 30 Jan 75 25 Cities as columns! Easy city comparison per month Key Insight: Pivoting transforms data from long format (many rows, few columns) to wide format (fewer rows, more columns). The index parameter becomes row labels, columns parameter becomes column headers, and values fill the cells. This restructuring enables easier cross-comparison and is essential for many data analysis and visualization tasks. TutorialsPoint - Reshape Data: Pivot | Pandas-like Pivot Implementation
Asked in
Meta 15 Google 12 Microsoft 10 Amazon 8
23.4K Views
Medium Frequency
~15 min Avg. Time
890 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