{ "data": { "question": { "questionId": "3073", "questionFrontendId": "2890", "boundTopicId": null, "title": "Reshape Data: Melt", "titleSlug": "reshape-data-melt", "content": "
\nDataFrame report
\n+-------------+--------+\n| Column Name | Type |\n+-------------+--------+\n| product | object |\n| quarter_1 | int |\n| quarter_2 | int |\n| quarter_3 | int |\n| quarter_4 | int |\n+-------------+--------+\n
\n\nWrite a solution to reshape the data so that each row represents sales data for a product in a specific quarter.
\n\nThe result format is in the following example.
\n\n\n
Example 1:
\n\n\nInput:\n+-------------+-----------+-----------+-----------+-----------+\n| product | quarter_1 | quarter_2 | quarter_3 | quarter_4 |\n+-------------+-----------+-----------+-----------+-----------+\n| Umbrella | 417 | 224 | 379 | 611 |\n| SleepingBag | 800 | 936 | 93 | 875 |\n+-------------+-----------+-----------+-----------+-----------+\nOutput:\n+-------------+-----------+-------+\n| product | quarter | sales |\n+-------------+-----------+-------+\n| Umbrella | quarter_1 | 417 |\n| SleepingBag | quarter_1 | 800 |\n| Umbrella | quarter_2 | 224 |\n| SleepingBag | quarter_2 | 936 |\n| Umbrella | quarter_3 | 379 |\n| SleepingBag | quarter_3 | 93 |\n| Umbrella | quarter_4 | 611 |\n| SleepingBag | quarter_4 | 875 |\n+-------------+-----------+-------+\nExplanation:\nThe DataFrame is reshaped from wide to long format. Each row represents the sales of a product in a quarter.\n\n", "translatedTitle": null, "translatedContent": null, "isPaidOnly": false, "difficulty": "Easy", "likes": 9, "dislikes": 1, "isLiked": null, "similarQuestions": "[]", "exampleTestcases": "{\"headers\":{\"report\":[\"product\",\"quarter_1\",\"quarter_2\",\"quarter_3\",\"quarter_4\"]},\"rows\":{\"report\":[[\"Umbrella\",417,224,379,611],[\"SleepingBag\",800,936,93,875]]}}", "categoryTitle": "Algorithms", "contributors": [], "topicTags": [], "companyTagStats": null, "codeSnippets": [ { "lang": "Pandas", "langSlug": "pythondata", "code": "import pandas as pd\n\ndef meltTable(report: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"165\", \"totalSubmission\": \"187\", \"totalAcceptedRaw\": 165, \"totalSubmissionRaw\": 187, \"acRate\": \"88.2%\"}", "hints": [ "Consider using a built-in function in pandas library to transform the data" ], "solution": { "id": "2105", "canSeeDetail": true, "paidOnly": false, "hasVideoSolution": false, "paidOnlyVideo": true, "__typename": "ArticleNode" }, "status": null, "sampleTestCase": "{\"headers\":{\"report\":[\"product\",\"quarter_1\",\"quarter_2\",\"quarter_3\",\"quarter_4\"]},\"rows\":{\"report\":[[\"Umbrella\",417,224,379,611],[\"SleepingBag\",800,936,93,875]]}}", "metaData": "{\n \"pythondata\": [\n \"report = pd.DataFrame([], columns=['product', 'quarter_1', 'quarter_2', 'quarter_3', 'quarter_4']).astype({'product':'object', 'quarter_1':'Int64', 'quarter_2':'Int64', 'quarter_3':'Int64', 'quarter_4':'Int64'})\"\n ],\n \"database\": true,\n \"name\": \"meltTable\",\n \"languages\": [\n \"pythondata\"\n ]\n}", "judgerAvailable": true, "judgeType": "large", "mysqlSchemas": [], "enableRunCode": true, "enableTestMode": false, "enableDebugger": true, "envInfo": "{\"pythondata\": [\"Pandas\", \"
Python 3.10 with Pandas 2.0.2 and NumPy 1.25.0
\"]}", "libraryUrl": null, "adminUrl": null, "challengeQuestion": null, "__typename": "QuestionNode" } } }