How to add a suffix to the row labels of a Series object

Overview

In pandas, the add_suffix() function adds a suffix to the row labels of a Series object.

Syntax

The add_suffix() function takes the syntax shown below:

add_suffix("XYZ")
Syntax for the add_suffix() function in pandas

Parameter value

The add_suffix() function takes a single required parameter value, suffix, which represents the string to add as a suffix to the row label of the Series object.

Return value

The add_suffix() function returns a new series with its row labels updated.

Example

Let's consider an example:

# A code to illustrate the add_suffix() function in pandas
# Importing the pandas library
import pandas as pd
# Creating a Series object
my_series = pd.Series([10, 20, 30, 40, 50])
# Printing the Series object
print(my_series)
# Adding a suffix to the row labels
print(my_series.add_suffix("_row"))

Explanation

  • Line 4: We import the pandas library.
  • Line 7: We create a Series object, my_series.
  • Line 10: We print my_series.
  • Line 13: We add the suffix "_row" to the row labels of my_series using the add_suffix() function. We print the result to the console.