{ "data": { "question": { "questionId": "3072", "questionFrontendId": "2889", "categoryTitle": "pandas", "boundTopicId": 2467493, "title": "Reshape Data: Pivot", "titleSlug": "reshape-data-pivot", "content": "
\nDataFrame weather\n+-------------+--------+\n| Column Name | Type   |\n+-------------+--------+\n| city        | object |\n| month       | object |\n| temperature | int    |\n+-------------+--------+\n
\n\n

Write a solution to pivot the data so that each row represents temperatures for a specific month, and each city is a separate column.

\n\n

The result format is in the following example.

\n\n

 

\n
\nExample 1:\nInput:\n+--------------+----------+-------------+\n| city         | month    | temperature |\n+--------------+----------+-------------+\n| Jacksonville | January  | 13          |\n| Jacksonville | February | 23          |\n| Jacksonville | March    | 38          |\n| Jacksonville | April    | 5           |\n| Jacksonville | May      | 34          |\n| ElPaso       | January  | 20          |\n| ElPaso       | February | 6           |\n| ElPaso       | March    | 26          |\n| ElPaso       | April    | 2           |\n| ElPaso       | May      | 43          |\n+--------------+----------+-------------+\nOutput:\n+----------+--------+--------------+\n| month    | ElPaso | Jacksonville |\n+----------+--------+--------------+\n| April    | 2      | 5            |\n| February | 6      | 23           |\n| January  | 20     | 13           |\n| March    | 26     | 38           |\n| May      | 43     | 34           |\n+----------+--------+--------------+\nExplanation:\nThe table is pivoted, each column represents a city, and each row represents a specific month.
\n", "translatedTitle": "数据重塑:透视", "translatedContent": "
\nDataFrame weather\n+-------------+--------+\n| Column Name | Type   |\n+-------------+--------+\n| city        | object |\n| month       | object |\n| temperature | int    |\n+-------------+--------+\n
\n\n

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

\n\n

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

\n\n

 

\n示例 1:\n\n
\n输入:\n+--------------+----------+-------------+\n| city         | month    | temperature |\n+--------------+----------+-------------+\n| Jacksonville | January  | 13          |\n| Jacksonville | February | 23          |\n| Jacksonville | March    | 38          |\n| Jacksonville | April    | 5           |\n| Jacksonville | May      | 34          |\n| ElPaso       | January  | 20          |\n| ElPaso       | February | 6           |\n| ElPaso       | March    | 26          |\n| ElPaso       | April    | 2           |\n| ElPaso       | May      | 43          |\n+--------------+----------+-------------+\n输出:\n+----------+--------+--------------+\n| month    | ElPaso | Jacksonville |\n+----------+--------+--------------+\n| April    | 2      | 5            |\n| February | 6      | 23           |\n| January  | 20     | 13           |\n| March    | 26     | 38           |\n| May      | 43     | 34           |\n+----------+--------+--------------+\n解释:\n表格被旋转,每一列代表一个城市,每一行代表特定的月份。
\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 1, "dislikes": 0, "isLiked": null, "similarQuestions": "[]", "contributors": [], "langToValidPlayground": "{\"cpp\": false, \"java\": false, \"python\": false, \"python3\": false, \"mysql\": false, \"mssql\": false, \"oraclesql\": false, \"c\": false, \"csharp\": false, \"javascript\": false, \"typescript\": false, \"bash\": false, \"php\": false, \"swift\": false, \"kotlin\": false, \"dart\": false, \"golang\": false, \"ruby\": false, \"scala\": false, \"html\": false, \"pythonml\": false, \"rust\": false, \"racket\": false, \"erlang\": false, \"elixir\": false, \"pythondata\": false, \"react\": false, \"vanillajs\": false, \"postgresql\": false}", "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef pivotTable(weather: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"1.1K\", \"totalSubmission\": \"1.5K\", \"totalAcceptedRaw\": 1130, \"totalSubmissionRaw\": 1506, \"acRate\": \"75.0%\"}", "hints": [ "Consider using a built-in function in pandas library to transform the data" ], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"weather\":[\"city\",\"month\",\"temperature\"]},\"rows\":{\"weather\":[[\"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]]}}", "metaData": "{\n \"pythondata\": [\n \"weather = pd.DataFrame([], columns=['city', 'month', 'temperature']).astype({'city':'object', 'month':'object', 'temperature':'Int64'})\"\n ],\n \"database\": true,\n \"name\": \"pivot_table\",\n \"languages\": [\n \"pythondata\"\n ],\n \"manual\": true\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "envInfo": "{\"pythondata\":[\"Pandas\",\"

Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0<\\/p>\"]}", "book": null, "isSubscribed": false, "isDailyQuestion": false, "dailyRecordStatus": null, "editorType": "CKEDITOR", "ugcQuestionId": null, "style": "LEETCODE", "exampleTestcases": "{\"headers\":{\"weather\":[\"city\",\"month\",\"temperature\"]},\"rows\":{\"weather\":[[\"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]]}}", "__typename": "QuestionNode" } } }