Pandas Series.add_suffix()


Pandas Series.add_suffix() method

In this tutorial, we'll go through examples for Series.add_suffix() method, which adds a suffix to the labels of a Pandas Series.

The syntax of Series.add_suffix(suffix) is:

Series.add_suffix(suffix, axis)

where

ParameterDescription
suffixThe string to add after each label.
axis[Optional]
Axis to add suffix on.
For example, {0 or ‘index’, 1 or ‘columns’, None}.
The default value is None.
Parameters of Series.add()

Examples

1. Add suffix "item_" to the index labels of Series

In this example, we shall take a Series object in series with three string values, add the suffix "_item" to the labels of the Series object.

Python Program

import pandas as pd

# Take a Series
series = pd.Series(["apple", "banana", "cherry"])

# Add a suffix to the labels
suffixed_series = series.add_suffix('_item')

# Print original and result Series
print("Original Series:")
print(series)

print("\nSeries after adding suffix:")
print(suffixed_series)

Output

Original Series:
0     apple
1    banana
2    cherry
dtype: object

Series after adding suffix:
0_item     apple
1_item    banana
2_item    cherry
dtype: object

Summary

In this tutorial, we have seen about Series.add_suffix() method is a convenient way to add a suffix to the labels of a Pandas Series.


Python Libraries