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 libraryimport pandas as pd# Creating a Series objectmy_series = pd.Series([10, 20, 30, 40, 50])# Printing the Series objectprint(my_series)# Adding a suffix to the row labelsprint(my_series.add_suffix("_row"))
Explanation
- Line 4: We import the
pandaslibrary. - 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 ofmy_seriesusing theadd_suffix()function. We print the result to the console.