Change language

Python | Pandas Series.rename ()

|

Series.rename() Pandas Series.rename() is used to change labels or index name Series for a given Series object.

Syntax: Series.rename (index = None, ** kwargs)

Parameter:
index: dict-like or functions are transformations to apply to the index
copy: Also copy underlying data
inplace: Whether to return a new Series. If True then value of copy is ignored.
level: In case of a MultiIndex, only rename labels in the specified level.

Returns: Series, DataFrame, or None

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

# import pandas as pd

import pandas as pd

 
# Create a 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 () to rename the name of this series object.

# rename the series

result = sr.rename ( ’Beverages’ )

 
# Print result

print (result)

Logout:


As we can see in the output, Series.rename () has successfully renamed this series object.

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

# pandas import 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 () to rename the level 0 of this series object.

# rename level 0

result = sr.rename (level = 0 , index = ’ Row_axis’ )

 
# Print result

print (result)

Output:

As we can see in the output, Series.rename () successfully renamed the 0th level of this series object.

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

Python | Pandas Series.rename () 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