Change language

Python | Pandas Dataframe.rename ()


The Pandas rename() method is used to rename any index, column or row. You can also rename a column using dataframe.columns = [#list] . But in the above case, there isn’t much freedom. Even if one column needs to be changed, the complete list of columns must be passed. Also, the above method does not apply to index marks.

Syntax: DataFrame.rename (mapper = None, index = None, columns = None, axis = None, copy = True, inplace = False, level = None)

mapper, index and columns: Dictionary value, key refers to the old name and value refers to new name. Only one of these parameters can be used at once.
axis: int or string value, 0 / ’row’ for Rows and 1 / ’columns’ for Columns.
copy: Copies underlying data if True.
inplace: Makes changes in original Data Frame if True.
level: Used to specify level in case data frame is having multiple level index.

Return Type: Data frame with new names

To load CSV, used in the code, click here.

Example # 1: Change index label

In this example, the name column is set as an index column and its name is changed later using the rename () method.

As shown in the output image, the names of the index marks in the first and second positions have been changed to NEW NAME & amp; NEW NAME 2.

Example # 2: Changing multiple column names

In this example, multiple column names are changed by passing in a dictionary. The result is later compared to the dataframe returned by the .columns method. Null values ​​are discarded before comparison, since NaN == NaN will return false.

# pandas module import

import pandas as pd

# create data frame from CSV file

data = pd.read_csv ( "nba.csv" , index_col = "Name" )

# modifying index columns using rename ()

data.rename (index = { "Avery Bradley" : "NEW NAME" ,

"Jae Crowder" : "NEW NAME 2" },

  in place = True )

# display

As shown in the output image, the results using both methods were the same because all values ​​are True.

Python | Pandas Dataframe.rename () Python functions: Questions

Python | Pandas Dataframe.rename () rename: Questions


Best laptop for Fortnite


Best laptop for Excel


Best laptop for Solidworks


Best laptop for Roblox


Best computer for crypto mining


Best laptop for Sims 4


Best laptop for Zoom


Best laptop for Minecraft


Latest questions


psycopg2: insert multiple rows with one query

12 answers


How to convert Nonetype to int or string?

12 answers


How to specify multiple return types using type-hints

12 answers


Javascript Error: IPython is not defined in JupyterLab

12 answers


Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method

# pandas module import

import pandas as pd

# create data frame from CSV file

data = pd.read_csv ( "nba.csv" , index_col = "Name" )


# change cols with rename ()

new_data = data.rename (col umns = { "Team" : "Team Name" ,

  " College " : " Education " ,

"Salary" : "Income" })

# modifying columns with .columns ()

data.columns = [ ’Team Name’ , ’Number’ , ’ Position’ , ’Age’ ,

  ’Height’ , ’ Weight’ , ’Education’ , ’ Income’ ]

# dropna is used to ignore n values ​​

print (new_data.dropna () = = data.dropna ())