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.
Parameters: 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.
data.rename (index = { "Avery Bradley" : "NEW NAME" ,
"Jae Crowder" : "NEW NAME 2" },
in place = True )
# display data
table>
Output: 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" )