Skip to content
Related Articles

Related Articles

Python | Pandas dataframe.rename_axis()
  • Last Updated : 16 Nov, 2018

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.

dataframe.rename_axis() is used to rename the axes of the index or columns in dataframe.

Syntax: DataFrame.rename_axis(mapper, axis=0, copy=True, inplace=False)

Parameters:
mapper : [scalar, list-like, optional] Value to set the axis name attribute.
axis : int or string, default 0
copy : [iboolean, default True] Also copy underlying data
inplace :boolean, default False

Returns: renamed : type of caller or None if inplace=True



For link to CSV file Used in Code, click here

Example #1: Replace team “Boston Celtics” with “Omega Warrior” in the nba.csv file




# importing pandas as pd
import pandas as pd
  
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
  
# Printing the first 10 rows of the 
# dataframe for visualization
df[:10]

Output:

We are going to change the row indexes and increase the value by twice.




# this will Increase the row index value by twice
df.rename_axis(lambda x:x * 2, axis ="index")

Output:

 

Example #2: Changing the column name




# importing pandas as pd
import pandas as pd
  
# Making data frame from the csv file
df = pd.read_csv("nba.csv")
  
# this will add '_X' at the end of each column name
df.rename_axis(lambda x:x+"_X", axis ="columns")

Output:

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :