Reshape Data: Pivot — LeetCode 2889 Python Solution
EasyPandas
- Problem
- #2889
- Reading time
- 2 min
- Source
- leetcode.com
Table schema
SQL
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
SQL
DataFrame weather
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| city | object |
| month | object |
| temperature | int |
+-------------+--------+Python solution
Python
import pandas as pd
def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
return weather.pivot(index='month', columns='city', values='temperature')Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) to O(n) auxiliary |
Related problems
Frequently asked questions
- How hard is LeetCode 2889. Reshape Data: Pivot?
- LeetCode 2889. Reshape Data: Pivot is rated Easy on LeetCode.
- What topics does LeetCode 2889. Reshape Data: Pivot cover?
- LeetCode 2889. Reshape Data: Pivot is tagged Pandas on LeetCode.