LeetCode 题解工作台

数据重塑:透视

DataFrame weather +-------------+--------+ | Column Name | Type | +-------------+--------+ | city | object | | month | object | | temperature | int | …

category

0

题型

code_blocks

1

代码语言

hub

0

相关题

当前训练重点

简单 · Reshape Data: Pivot core interview pattern

bolt

答案摘要

import pandas as pd def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:

Interview AiBox logo

Interview AiBox 实时 AI 助手,陪你讲清 Reshape Data: Pivot core interview pattern 题型思路

试试 AiBox 面试助手arrow_forward
description

题目描述

DataFrame weather
+-------------+--------+
| Column Name | Type   |
+-------------+--------+
| city        | object |
| month       | object |
| temperature | int    |
+-------------+--------+

编写一个解决方案,以便将数据 旋转,使得每一行代表特定月份的温度,而每个城市都是一个单独的列。

输出结果格式如下示例所示。

 

示例 1:
输入:
+--------------+----------+-------------+
| 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          |
+--------------+----------+-------------+
输出:
+----------+--------+--------------+
| month    | ElPaso | Jacksonville |
+----------+--------+--------------+
| April    | 2      | 5            |
| February | 6      | 23           |
| January  | 20     | 13           |
| March    | 26     | 38           |
| May      | 43     | 34           |
+----------+--------+--------------+
解释:
表格被旋转,每一列代表一个城市,每一行代表特定的月份。
lightbulb

解题思路

方法一

1
2
3
4
5
6
import pandas as pd


def pivotTable(weather: pd.DataFrame) -> pd.DataFrame:
    return weather.pivot(index='month', columns='city', values='temperature')
speed

复杂度分析

指标
时间and space complexity depend on the number of rows and columns in the table. Pivoting is typically O(n) in time for scanning all rows and O(c*m) in space for storing the new table, where c is the number of cities and m is the number of months.
空间Depends on the final approach
psychology

面试官常问的追问

外企场景
  • question_mark

    Do you recognize this as a data reshaping or pivot problem?

  • question_mark

    Can you suggest a pandas function that avoids manual aggregation?

  • question_mark

    How would you handle missing city-month temperature entries?

warning

常见陷阱

外企场景
  • error

    Forgetting to set the correct index, leading to a misaligned table.

  • error

    Not sorting months after pivoting, which may cause tests to fail.

  • error

    Overwriting original data instead of creating a clean pivoted table.

swap_horiz

进阶变体

外企场景
  • arrow_right_alt

    Pivot by multiple indices such as year and month to handle multi-year data.

  • arrow_right_alt

    Aggregate temperatures by mean or max if multiple entries exist per city-month.

  • arrow_right_alt

    Pivot with additional columns like humidity or rainfall instead of temperature.

help

常见问题

外企场景

数据重塑:透视题解:Reshape Data: Pivot cor… | LeetCode #2889 简单