Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 41898b2

Browse files
Add file
1 parent 3076e1d commit 41898b2

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /