Updated April 6, 2023
Introduction to Pandas DataFrame.rename()
Every data structure which has labels to it will hold the necessity to manipulate the labels, In a tabular data structure like dataframe these labels are declared at both the row level and column level. So if there is a need to manipulate these labels both at the row and column level then rename() method can be used. The rename() method offers the flexibility to sophisticatedly manipulate the column level headers and row-level indexes in the dataframe.
Syntax:
DataFrame.rename(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')
Parameter & Description of Pandas DataFrame.rename()
Below are the parameters of Pandas DataFrame.rename() in Python:
Parameter | Description |
Mapper | The mapper holds the values which needs to be replaced and their replacement values, So the old value and the corresponding new value which needs to be replaced will be specified here. The mapper argument usually takes values in the form of a dictionary. Eg; {“A”: “Header1”, “B”: “Header2”} The above dictionary when passed in the rename function it implies that the value ‘A’ in either the column or the index needs to be replaced as Header1 and similarly the column or index with value ‘B’ needs to be replaced with the new value ‘Header2’. When using the Mapper argument it needs to be combined with the axis argument. The axis argument here mentions whether the change is for the column or the index. The description of this argument is explained below separately. |
Index | This is an alternative to the argument axis ( mapper, axis=0 ) , Here Index represents the rows of the dataframe. So every rename values which are mentioned here will be applied to the rows of the dataframe. |
Columns | This is again an another alternative to the argument axis ( mapper, axis=1 ), Here columns as the name suggest it represents the columns of the dataframe. So every rename values which are mentioned here will be applied to the column names of the dataframe. |
Axis | This argument represents the column or the axis upon which the Rename() function needs to be applied on. The value specified in this argument represents either a column position or a row position in the dataframe. to achieve this capability to flexibly travel over a dataframe the axis value is framed on below means ,{index (0), columns (1)} . here mentioning the value of 0 to axis argument fills the rename values for each and every row in the dataframe, whereas mentioning the value of 1 in the dataframe fills the replacement values for all the columns in the dataframe. |
Copy | Refers whether the data in the dataframe needs to be copied along with the operation which is been performed. |
Inplace | This is used to determine whether the operation need to be performed at the place of the data. So this means whether the outcome of the rename() need to be performed directly on to the current dataframe for which it is applied. This is a boolean variable , if this is set to true then the rename process will be applied to the current dataframe itself, if this argument is assigned as false then no changes will be applied to the current dataframe a equals relation can be used to pull the updated dataframe values into a different dataframe. |
Level | if the axis is a MultiIndex (hierarchical), count along with a particular level. |
Errors | Represents whether an exception needs to be raised or not. Majorly this option allows to control whether an exception has to be raised or not on a case where an exception could be validly occurring. ( Need to be exceptionally cautious while setting the value of the error parameter is been raise or ignore ).
Raise: When the raise parameter is set it allows the exception to be validly raised. Ignore: The Ignore option allows an occurring exception to be controlled and ignored from being raised. |
Examples to Implement of Pandas DataFrame.rename()
Below are the examples of Pandas DataFrame.rename():
Example #1
Code:
import pandas as pd
import numpy as np
Core_Dataframe = pd.DataFrame({'Emp_No' : ['Emp1', np.nan,'Emp3','Emp4'],
'Employee_Name' : ['Arun', 'selva', np.nan, 'arjith'],
'Employee_dept' : ['CAD', 'CAD', 'DEV', np.nan]})
print(" THE CORE DATAFRAME BEFORE RENAME OPERATION ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER RENAME OPERATION ")
print(Core_Dataframe.rename(columns= { 'Emp_No' : 'Employee_Number' ,
'Employee_dept' : 'Employee_Department'
},
index= { 0 : 'Row1',
1 : 'Row2',
2 : 'Row3',
3 : 'Row4'}))
print("")
Output:
Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds details like employee number, employee name, and employee department. The rename() method is used for manipulating the column names and the row values of the dataframe. Here the index and column parameters are used for making these changes to the headers. The manipulated dataframe is again printed on to the console and from the printed output we can notice that the changes are applied upon the column names and their index values.
Example #2
Code:
import pandas as pd
import numpy as n
Core_Dataframe = pd.DataFrame({'A' : [ 1, 6, 11, 15, 21, 26],
'B' : [2, 7, 12, 17, 22, 27],
'C' : [3, np.nan, 13, 18, 23, 28],
'D' : [4, 9, 14, 19, np.nan, 29],
'E' : [5, 10, 15, np.nan, 25, 30]})
print(" THE CORE DATAFRAME BEFORE RENAME OPERATION ")
print(Core_Dataframe)
print("")
print(" THE CORE DATAFRAME AFTER RENAME OPERATION ")
Core_Dataframe.rename(mapper={'A' : 'Header1',
'B' : 'Header2',
'C' : 'Header3',
'D' : 'Header4',
'E' : 'Header5'},axis=1,inplace=True)
print(Core_Dataframe)
Output:
Explanation: In this example, the core dataframe is first formulated. pd.dataframe() is used for formulating the dataframe. Every row of the dataframe is inserted along with their column names. Once the dataframe is completely formulated it is printed on to the console. We can notice at this instance the dataframe holds a random set of numbers. Here the rename() method is used for renaming the columns of the dataframe and the rename operation is applied upon the column values using the axis technique. so setting the axis value as 1 represents the columns in the dataframe. The output is printed on to the console and we can notice that the header values are changed as expected in the core dataframe itself.
Recommended Articles
We hope that this EDUCBA information on “Pandas DataFrame.rename()” was beneficial to you. You can view EDUCBA’s recommended articles for more information.