Pandas DataFrame.rename()
The main task of the Pandas rename() function is to rename any index, column, or row . This method is useful for renaming some selected columns because we have to specify the information only for those columns that we want to rename.
It mainly alters the axes labels based on some of the mapping (dict or Series) or the arbitrary function. The function must be unique and should range from 1 to -1 . The labels will be left, if it is not contained in a dict or Series. If you list some extra labels, it will throw an error.
Syntax:
- DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors=‘ignore’)
Parameters:
- mapper: It is a dict-like or function transformation that is to be applied to a particular axis label. We can use either mapper or axis to specify the axis targeted with mapper, index , and
- index: It is an alternative of specifying the axis (mapper, axis =0 is equivalent to the index=mapper ).
- columns: It is an alternative to specify an axis (mapper, axis =1 is equivalent to the columns=mapper ).
- axis: It refers to an int or str value that defines the axis targeted with the mapper . It can be either the axis name (‘index’, ‘columns’) or the number.
- copy: It refers to a boolean value that copies the underlying data. The default value of the copy is True.
- inplace: It refers to a boolean value and checks whether to return the new DataFrame or not. If it is true, it makes the changes in the original DataFrame. The default value of the inplace is True.
- level: It refers to an int or level name values that specify the level, if DataFrame has a multiple level index. The default value of the level is None.
- errors: It refers to ignore, raise If we specify raise value, it raises a KeyError if any of the labels are not found in the selected axis.
Returns:
It returns the DataFrame with renamed axis labels.
Example 1: The below example renames a single column:
import pandas as pd # Define a dictionary containing information of employees info = {'name': ['Parker', 'Smith', 'William', 'Robert'], 'age': [38, 47, 44, 34], 'language': ['Java', 'Python', 'JavaScript', 'Python']} # Convert dictionary into DataFrame info_pd = pd.DataFrame(info) # Before renaming columns print(info_pd) info_pd.rename(columns = {'name':'Name'}, inplace = True) # After renaming columns print("\nAfter modifying first column:\n", info_pd.columns
Output:
name age language
0 Parker 38 Java
1 Smith 47 Python
2 William 44 JavaScript
3 Robert 34 Python
After modifying first column:
Index(['Name', 'age', 'language'], dtype='object')
Example2: The below example renames the multiple columns:
import pandas as pd # Define a dictionary containing information of employees info = {'name': ['Parker', 'Smith', 'William', 'Robert'], 'age': [38, 47, 44, 34], 'language': ['Java', 'Python', 'JavaScript', 'Python']} # Convert dictionary into DataFrame info_pd = pd.DataFrame(info) # Before renaming columns print(info_pd) info_pd.rename(columns = {'name':'Name', 'age':'Age', 'language':'Language'}, inplace = True) # After renaming columns print(info_pd.columns)
Output:
name age language
0 Parker 38 Java
1 Smith 47 Python
2 William 44 JavaScript
3 Robert 34 Python
Index(['Name', 'Age', 'Language'], dtype='object')
Example3: The below example renames indexes of a particular column:
import pandas as pd data = {'Name': ['Smith', 'Parker', 'William'], 'Emp_ID': [101, 102, 103], 'Language': ['Python', 'Java', 'JavaScript']} info1 = pd.DataFrame(data) print('DataFrame:\n', info1) info2 = info.rename(index={0: '#0', 1: '#1', 2: '#2'}) print('Renamed Indexes:\n', info2)
Output:
DataFrame:
Name Emp_ID Language
0 Smith 101 Python
1 Parker 102 Java
2 William 103 JavaScript
Renamed Indexes:
Name Emp_ID Language
#0 Smith 101 Python
#1 Parker 102 Java
#2 William 103 JavaScript