




Sponsored
Sponsored
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.
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.
1import pandas as pd
2
3def pivot_weather(weather):
4
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.
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.
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.
1#include <stdio.h>
2#include <string.h>
3
4#define MAX_CITIES 100
5#define MAX_MONTHS 12
6
7typedef struct {
8    char month[20];
9    int temperature[MAX_CITIES];
10} PivotRow;
11
12void pivot(char cities[][20], char months[][20], int temperatures[], int len) {
13    PivotRow data[MAX_MONTHS];
14    char unique_cities[MAX_CITIES][20];
15    int city_count = 0;    
16    int pivot[12][2] = {0};
17    int row = 0;
18
19    for (int i = 0; i < len; i++) {
20        int city_index = -1;
21        for (int j = 0; j < city_count; j++) {
22            if (strcmp(cities[i], unique_cities[j]) == 0) {
23                city_index = j;
24                break;
25            }
26        }
27        
28        if (city_index == -1) {
29            strcpy(unique_cities[city_count], cities[i]);
30            city_index = city_count++;
31        }
32        
33        int month_index = -1;
34        for (int k = 0; k < row; k++) {
35            if (strcmp(data[k].month, months[i]) == 0) {
36                month_index = k;
37                break;
38            }
39        }
40
41        if (month_index == -1) {
42            strcpy(data[row].month, months[i]);
43            data[row].temperature[city_index] = temperatures[i];
44            row++;
45        } else {
46            data[month_index].temperature[city_index] = temperatures[i];
47        }
48    }
49
50    printf("%12s", "month");
51    // Print city headers
52    for (int i = 0; i < city_count; i++) {
53        printf("%12s", unique_cities[i]);
54    }
55    printf("\n");
56    
57    // Print data
58    for (int i = 0; i < row; i++) {
59        printf("%12s", data[i].month);
60        for (int j = 0; j < city_count; j++) {
61            printf("%12d", data[i].temperature[j]);
62        }
63      
64        printf("\n");
65    }
66}
67
68int main() {
69    char cities[10][20] = {"Jacksonville", "Jacksonville", "Jacksonville", "Jacksonville", "Jacksonville", "ElPaso", "ElPaso", "ElPaso", "ElPaso", "ElPaso"};
70    char months[10][20] = {"January", "February", "March", "April", "May", "January", "February", "March", "April", "May"};
71    int temperatures[10] = {13, 23, 38, 5, 34, 20, 6, 26, 2, 43};
72    int len = 10;
73    pivot(cities, months, temperatures, len);
74    return 0;
75}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.