Javatpoint Logo
Javatpoint Logo

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:

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:

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:

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:

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

Next TopicDataFrame.sample()




Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA