2024-01-09|閱讀時間 ‧ 約 0 分鐘

Reshape Data: Pivot 資料表的樞紐轉換_Intro to Pandas

題目敘述

題目會給定一個pandas DataFrame作為輸入,要求我們以原有的資料表為基礎,將資料表做樞紐轉換,垂直方向月份水平方向是不同的城市,而表格內容該城市在某個月份的溫度


題目的原文敘述


測試範例

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.
垂直方向是月份,水平方向是不同的城市,而表格內容是該城市在某個月份的溫度。


演算法

分享至
成為作者繼續創作的動力吧!
© 2024 vocus All rights reserved.