1
+ """
2
+ 2889. Reshape Data: Pivot
3
+ Solved
4
+ Easy
5
+ Companies
6
+ Hint
7
+ DataFrame weather
8
+ +-------------+--------+
9
+ | Column Name | Type |
10
+ +-------------+--------+
11
+ | city | object |
12
+ | month | object |
13
+ | temperature | int |
14
+ +-------------+--------+
15
+ Write a solution to pivot the data so that each row represents temperatures for a specific month, and each city is a separate column.
16
+
17
+ The result format is in the following example.
18
+
19
+ Example 1:
20
+ Input:
21
+ +--------------+----------+-------------+
22
+ | city | month | temperature |
23
+ +--------------+----------+-------------+
24
+ | Jacksonville | January | 13 |
25
+ | Jacksonville | February | 23 |
26
+ | Jacksonville | March | 38 |
27
+ | Jacksonville | April | 5 |
28
+ | Jacksonville | May | 34 |
29
+ | ElPaso | January | 20 |
30
+ | ElPaso | February | 6 |
31
+ | ElPaso | March | 26 |
32
+ | ElPaso | April | 2 |
33
+ | ElPaso | May | 43 |
34
+ +--------------+----------+-------------+
35
+ Output:
36
+ +----------+--------+--------------+
37
+ | month | ElPaso | Jacksonville |
38
+ +----------+--------+--------------+
39
+ | April | 2 | 5 |
40
+ | February | 6 | 23 |
41
+ | January | 20 | 13 |
42
+ | March | 26 | 38 |
43
+ | May | 43 | 34 |
44
+ +----------+--------+--------------+
45
+ Explanation:
46
+ The table is pivoted, each column represents a city, and each row represents a specific month."
47
+ """
48
+
49
+ import pandas as pd
50
+
51
+ def pivotTable (weather : pd .DataFrame ) -> pd .DataFrame :
52
+ return weather .pivot_table (values = 'temperature' , index = 'month' , columns = 'city' )
0 commit comments