Skip to content

Practice Reshape Data: Pivot

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Easy#2889
LeetCode Problem

Reshape Data: Pivot

Problem Statement

The weather DataFrame has city (object), month (object) and temperature (int) columns. Pivot the data so each row corresponds to a month and each city becomes a separate column containing that city's temperature for the month.

Source: Reshape Data: Pivot on LeetCode. Use the original task for its coding environment and submissions.

Examples

# 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           | +----------+--------+--------------+

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.