-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2889-ReshapeDataPivot.py
More file actions
66 lines (60 loc) · 2.71 KB
/
2889-ReshapeDataPivot.py
File metadata and controls
66 lines (60 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# 2889. Reshape Data: Pivot
# 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.
import pandas as pd
def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
# index:确定新 DataFrame 中的行。在本例中,我们使用原始 DataFrame 中的 month 列作为索引,这意味着我们的透视表将为 month 列中的每个唯一值都建立行。
# columns:确定新 DataFrame 中的列。在这里,我们使用的是 city 列,这意味着我们的透视表将有一列对应于 city 列中的每个唯一值。
# values:指定重塑表格时要使用的值。在本例中,我们使用原始 DataFrame 中的 temperature 列
#return pd.DataFrame(weather.pivot(index='month', columns='city', values='temperature'))
return weather.pivot(index='month', columns='city', values='temperature')
if __name__ == "__main__":
l = [
["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 ],
]
print(pivotTable(pd.DataFrame(l,columns=["city","month","temperature"])))