DataFrame weather
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| city | object |
| month | object |
| temperature | int |
+-------------+--------+
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 format is in the following example.
Example 1:
Input:
+--------------+----------+-------------+
| city | month | temperature |
+--------------+----------+-------------+
| Jacksonville | January | 13 |
| Jacksonville | February | 23 |
| Jacksonville | March | 38 |
| Jacksonville | April | 5 |
| Jacksonville | May | 34 |
| ElPaso | January | 20 |
| ElPaso | February | 6 |
| ElPaso | March | 26 |
| ElPaso | April | 2 |
| ElPaso | May | 43 |
+--------------+----------+-------------+
Output:
+----------+--------+--------------+
| month | ElPaso | Jacksonville |
+----------+--------+--------------+
| April | 2 | 5 |
| February | 6 | 23 |
| January | 20 | 13 |
| March | 26 | 38 |
| May | 43 | 34 |
+----------+--------+--------------+
Explanation:
The table is pivoted, each column represents a city, and each row represents a specific month.
This approach involves using the Pandas library in Python to pivot the data. Pandas offers a built-in pivot function that can easily transform your dataframe, setting the index, columns, and values appropriately.
This Python solution uses the Pandas library to pivot the data. The pivot function is called with the index set to 'month', columns set to 'city', and values set to 'temperature'. This means that each row is grouped by the 'month' field, and temperature data is organized into columns corresponding to each 'city'. Finally, reset_index() is applied to convert the row indices back into a column format.
Time Complexity: O(n) - where n is the number of records in the dataframe, as we essentially have to process each record once.
Space Complexity: O(n) - the space used by the output structure is linear relative to the input.
This approach involves manually restructuring the input data using arrays and dictionaries to simulate the pivot operation. This example is demonstrated in the C programming language.
This C code manually aggregates the input data into a new structure that acts as a pivot table. It uses arrays to store unique cities and a static array to hold the results. The cities are dynamically identified, and their index is used to populate the 'temperature' attribute of each month's data with appropriate values. The transformation is effectively a 'pivot' operation achieved by nested loops and simple data structures in C.
Time Complexity: O(m * n) - where m is the number of unique months and n is the number of unique cities, primarily driven by the nested looping.
Space Complexity: O(m*n) - the result table's space use is directly proportional to the input size, considering unique month and city combinations.
| Approach | Complexity |
|---|---|
| Approach 1: Using Pandas Library in Python | Time Complexity: O(n) - where n is the number of records in the dataframe, as we essentially have to process each record once. |
| Approach 2: Manual Aggregation and Restructuring in C | Time Complexity: O(m * n) - where m is the number of unique months and n is the number of unique cities, primarily driven by the nested looping. |
The LeetCode Fallacy • NeetCode • 628,353 views views
Watch 9 more video solutions →Practice Reshape Data: Pivot with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor