Change language

Python | Pandas Series.rename_axis ()

|

Series.rename_axis() Pandas Series.rename_axis() is used to set the axis name for the index, or columns.

Syntax: Series.rename_axis (mapper = None, index = None, columns = None, axis = None, copy = True, inplace = False)

Parameter:
mapper: Value to set the axis name attribute.
index, columns: A scalar, list-like, dict-like or functions transformations to apply to that axis’ values.
axis: The axis to rename.
copy: Also copy underlying data.
inplace: Modifies the object directly, instead of creating a new Series or DataFrame.

Returns: Series, DataFrame, or None

Example # 1: Use Series.rename_axis () to rename the axis of this Series object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ 10 , 25 , 3 , 11 , 24 , 6 ])

 
# Create index

index_ = [ ’Coca Cola’ , ’Sprite’ , ’Coke’ , ’ Fanta’ , ’Dew’ , ’ ThumbsUp’ ]

 
# set index

sr.index = index_

 
# Print series

print (sr)

Output:

We will now use Series. rename_axis () to rename the axis of the given object series object.

# rename axis

result = sr.rename_axis ( ’Beverages’ )

 
# Print result

print (result)

Output:


As we can see in the output, Series.rename_axis () successfully renamed the axis of this series object.

Example # 2: Use Series.rename_axis () to rename the MultiIndex axis of this Series object.

# import pandas as pd

import pandas as pd

 
# Create series

sr = pd.Series ([ ’New York’ , ’ Chicago’ , ’Toronto’ , ’Lisbon’ , ’ Rio’ ])

  
# Create MultiIndex

index_ = pd.MultiIndex.from_product ([[ ’Names’ ], [ ’ City 1’ , ’City 2 ’ , ’ City 3’ , ’City 4’ , ’ City 5’ ]],

names = [ ’Level 1’ , ’ Level 2’ ])

 
# set index

sr.index = index_

 
# Print series

print (sr)  

Output:

We will now use Series.rename_axis () to rename the axis of this series object .

# rename both axis levels
# this series object

result = sr.rename_axis ([ ’First_level’ , ’ Second_level’ ] )

 
# Print result

print (result)

Exit d:

As we can see in the output, Series.rename_axis () has successfully renamed both axis levels of this series object.

Python | Pandas Series.rename_axis () Python functions: Questions

Python | Pandas Series.rename_axis () rename: Questions

Shop

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

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () 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