{"id":469,"date":"2023-11-16T12:04:39","date_gmt":"2023-11-16T12:04:39","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=469"},"modified":"2023-11-22T11:02:31","modified_gmt":"2023-11-22T11:02:31","slug":"rename-column-pandas","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/rename-column-pandas\/","title":{"rendered":"How to Rename a Column in Pandas (with code)"},"content":{"rendered":"\n<p>Changing the name of Pandas columns is a common task that often arises when working with data, whether you need to standardize column names, make them more descriptive, or simply prefer a different naming convention. In this article, we will explore how to rename a single column or multiple columns in Pandas.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Pandas rename() Function<\/strong><\/h2>\n\n\n\n<p>Renaming columns in a data frame can be beneficial for various reasons, such as improving readability, maintaining consistency, or aligning with specific naming conventions. <\/p>\n\n\n\n<p><strong>The rename() function in Pandas allows you to rename one or more columns by providing a dictionary-like object that maps the old column names to the new ones.<\/strong><\/p>\n\n\n\n<p>If you want to remove columns, you can use the <a href=\"https:\/\/favtutor.com\/articles\/pandas-drop-column\/\">drop() method<\/a>.<\/p>\n\n\n\n<p>Here&#8217;s a basic syntax for the rename function:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None)<\/code><\/pre>\n\n\n\n<p>Here\u2019s a breakdown of the syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>mapper<\/strong>: Dictionary-like or function. It&#8217;s used to specify the mapping of old names to new names. If it&#8217;s a dictionary, keys are the current column\/index names, and values are the new names. If it&#8217;s a function, it should take a column\/index name and return a new name.<br><\/li>\n\n\n\n<li><strong>index and columns<\/strong>: These parameters are used to specifically rename the index or columns, respectively. You can provide a dictionary or a function similar to the mapper parameter.<br><\/li>\n\n\n\n<li><strong>axis<\/strong>: Specifies whether to rename the index (axis=0), columns (axis=1), or both (axis=None, which is the default).<br><\/li>\n\n\n\n<li><strong>copy<\/strong>: If True (default), it creates a new DataFrame with the updated names. If False, it modifies the original DataFrame in place.<br><\/li>\n\n\n\n<li><strong>inplace<\/strong>: If True, it modifies the DataFrame in place and returns None. If False (default), it returns a new DataFrame with the updated names.<br><\/li>\n\n\n\n<li><strong>leve<\/strong>l: For DataFrames with hierarchical indexing, this parameter specifies the level to rename.<br><\/li>\n<\/ul>\n\n\n\n<p>Now let us discuss the various methods of using the rename() method in Python Pandas.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Renaming a Single Column<\/strong><\/h3>\n\n\n\n<p>To rename a single column, you can specify the old column name as the key and the new column name as the value in the rename() function.&nbsp;<\/p>\n\n\n\n<p>Let&#8217;s consider the following example in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade'], 'age': [25, 30, 35]}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Rename the column 'name'\ndf.rename(columns={'name': 'full_name'}, inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age\n0  John   25\n1  Jane   30\n2  Jade   35\n\n\nModified DataFrame:\n   full_name  age\n0      John   25\n1      Jane   30\n2      Jade   35\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Renaming Multiple Columns<\/strong><\/h3>\n\n\n\n<p>To rename multiple columns, you can provide a dictionary-like object with the old column names as keys and the new column names as values.<\/p>\n\n\n\n<p>Let&#8217;s consider the following example in Python:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade'], 'age': [25, 30, 35], 'city': ['New York', 'London', 'Paris']}\ndf = pd.DataFrame(data)\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Rename the columns 'name', 'city'\ndf.rename(columns={'name': 'full_name', 'city': 'location'}, inplace=True)\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age      city\n0  John   25  New York\n1  Jane   30    London\n2  Jade   35     Paris\n\n\nModified DataFrame:\n   full_name  age  location\n0      John   25  New York\n1      Jane   30    London\n2      Jade   35     Paris\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Assigning a List of New Column Names<\/strong><\/h3>\n\n\n\n<p>There are various methods to rename columns in Pandas Python other than using the rename function. <strong>Another approach to renaming columns in a Pandas DataFrame is by directly assigning a list containing the new column names to the columns attribute of the DataFrame object. <\/strong>This method is useful when you want to rename all the columns or a subset of them.<\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade'], 'age': [25, 30, 35]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# Rename both the &quot;name&quot; and &quot;age&quot; columns to &quot;full_name&quot; and &quot;years_old&quot;\ndf.columns = ['full_name', 'years_old']\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age\n0  John   25\n1  Jane   30\n2  Jade   35\n\n\nModified DataFrame:\n   full_name  years_old\n0      John         25\n1      Jane         30\n2      Jade         35\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using the add_prefix() and add_suffix()<\/strong><\/h3>\n\n\n\n<p>The add_prefix() and add_suffix() functions in Pandas allow you to add a prefix or suffix to the existing column names. These functions are useful when you want to maintain the original column names but add additional information to differentiate them.<\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade'], 'age': [25, 30, 35]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# To add a prefix &quot;user_&quot; and a suffix &quot;_info&quot; to the column names\ndf = df.add_prefix('user_')\ndf = df.add_suffix('_info')\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age\n0  John   25\n1  Jane   30\n2  Jade   35\n\n\nModified DataFrame:\n   user_name_info  user_age_info\n0           John             25\n1           Jane             30\n2           Jade             35\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Using Dataframe.columns.str.replace<\/strong><\/h3>\n\n\n\n<p>Pandas provides the Dataframe.columns.str.replace method, which allows you to replace specific texts within column names. This method is useful when you want to replace certain substrings or characters with new values in column names.<\/p>\n\n\n\n<p>Consider the following example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import pandas as pd\ndata = {'name': ['John', 'Jane', 'Jade'], 'age': [25, 30, 35]}\ndf = pd.DataFrame(data)\n\n# Display the original DataFrame\nprint('Original DataFrame:\\n', df)\n\n# To replace the substring &quot;name&quot; with &quot;full_name&quot; and &quot;age&quot; with &quot;years_old&quot; in the column names\ndf.columns = df.columns.str.replace('name', 'full_name')\ndf.columns = df.columns.str.replace('age', 'years_old')\n\n# Display the modified DataFrame\nprint('Modified DataFrame:\\n', df)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>Original DataFrame:\n    name  age\n0  John   25\n1  Jane   30\n2  Jade   35\n\n\nModified DataFrame:\n   full_name  years_old\n0      John         25\n1      Jane         30\n2      Jade         35\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we explored many different methods to rename columns, including using the rename() function, assigning a list of new column names, using the set_axis() function, and adding prefixes and suffixes with add_prefix() and add_suffix(). By mastering these techniques, you can easily manipulate column names in your DataFrame to align with your data analysis needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Want to change the name of a Pandas column? Learn how to rename a single or multiple columns in Pandas with Python code.<\/p>\n","protected":false},"author":10,"featured_media":472,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[35],"tags":[36,37,32],"class_list":["post-469","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-data-science","tag-data-science","tag-pandas","tag-python"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/469","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=469"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/469\/revisions"}],"predecessor-version":[{"id":530,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/469\/revisions\/530"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/472"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}