{ "data": { "question": { "questionId": "3068", "questionFrontendId": "2885", "categoryTitle": "pandas", "boundTopicId": 2467494, "title": "Rename Columns", "titleSlug": "rename-columns", "content": "
\nDataFrame students
\n+-------------+--------+\n| Column Name | Type |\n+-------------+--------+\n| id | int |\n| first | object |\n| last | object |\n| age | int |\n+-------------+--------+\n
\n\nWrite a solution to rename the columns as follows:
\n\nid
to student_id
first
to first_name
last
to last_name
age
to age_in_years
The result format is in the following example.
\n\n\n
\nExample 1:\nInput:\n+----+---------+----------+-----+\n| id | first | last | age |\n+----+---------+----------+-----+\n| 1 | Mason | King | 6 |\n| 2 | Ava | Wright | 7 |\n| 3 | Taylor | Hall | 16 |\n| 4 | Georgia | Thompson | 18 |\n| 5 | Thomas | Moore | 10 |\n+----+---------+----------+-----+\nOutput:\n+------------+------------+-----------+--------------+\n| student_id | first_name | last_name | age_in_years |\n+------------+------------+-----------+--------------+\n| 1 | Mason | King | 6 |\n| 2 | Ava | Wright | 7 |\n| 3 | Taylor | Hall | 16 |\n| 4 | Georgia | Thompson | 18 |\n| 5 | Thomas | Moore | 10 |\n+------------+------------+-----------+--------------+\nExplanation: \nThe column names are changed accordingly.\n", "translatedTitle": "重命名列", "translatedContent": "
\nDataFrame students
\n+-------------+--------+\n| Column Name | Type |\n+-------------+--------+\n| id | int |\n| first | object |\n| last | object |\n| age | int |\n+-------------+--------+\n
\n\n编写一个解决方案,按以下方式重命名列:
\n\nid
重命名为 student_id
first
重命名为 first_name
last
重命名为 last_name
age
重命名为 age_in_years
返回结果格式如下示例所示。
\n\n\n\n
示例 1:
\n\n\n输入:\n+----+---------+----------+-----+\n| id | first | last | age |\n+----+---------+----------+-----+\n| 1 | Mason | King | 6 |\n| 2 | Ava | Wright | 7 |\n| 3 | Taylor | Hall | 16 |\n| 4 | Georgia | Thompson | 18 |\n| 5 | Thomas | Moore | 10 |\n+----+---------+----------+-----+\n输出:\n+------------+------------+-----------+--------------+\n| student_id | first_name | last_name | age_in_years |\n+------------+------------+-----------+--------------+\n| 1 | Mason | King | 6 |\n| 2 | Ava | Wright | 7 |\n| 3 | Taylor | Hall | 16 |\n| 4 | Georgia | Thompson | 18 |\n| 5 | Thomas | Moore | 10 |\n+------------+------------+-----------+--------------+\n解释:\n列名已相应更换。\n", "isPaidOnly": false, "difficulty": "Easy", "likes": 0, "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 renameColumns(students: pd.DataFrame) -> pd.DataFrame:\n ", "__typename": "CodeSnippetNode" } ], "stats": "{\"totalAccepted\": \"1.4K\", \"totalSubmission\": \"1.7K\", \"totalAcceptedRaw\": 1365, \"totalSubmissionRaw\": 1679, \"acRate\": \"81.3%\"}", "hints": [ "Consider using a build-in function in pandas library with a dictionary to rename the columns as specified." ], "solution": null, "status": null, "sampleTestCase": "{\"headers\":{\"students\":[\"id\",\"first\",\"last\",\"age\"]},\"rows\":{\"employees\":[],\"students\":[[1,\"Mason\",\"King\",6],[2,\"Ava\",\"Wright\",7],[3,\"Taylor\",\"Hall\",16],[4,\"Georgia\",\"Thompson\",18],[5,\"Thomas\",\"Moore\",10]]}}", "metaData": "{\n \"pythondata\": [\n \"students = pd.DataFrame([], columns=['id', 'first', 'last', 'age']).astype({'id':'Int64', 'first':'object', 'last':'object', 'age':'Int64'})\\n\"\n ],\n \"database\": true,\n \"name\": \"renameColumns\",\n \"languages\": [\n \"pythondata\"\n ]\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\":{\"students\":[\"id\",\"first\",\"last\",\"age\"]},\"rows\":{\"employees\":[],\"students\":[[1,\"Mason\",\"King\",6],[2,\"Ava\",\"Wright\",7],[3,\"Taylor\",\"Hall\",16],[4,\"Georgia\",\"Thompson\",18],[5,\"Thomas\",\"Moore\",10]]}}", "__typename": "QuestionNode" } } }