Pandas Series.add_prefix()
Pandas Series.add_prefix() method
In this tutorial, we'll go through examples for Series.add_prefix() method, which adds a prefix to the labels of a Pandas Series.
The syntax of Series.add_prefix(prefix) is:
Series.add_prefix(prefix, axis)where
| Parameter | Description |
|---|---|
prefix | The string to add before each label. |
axis | [Optional] Axis to add prefix on. For example, {0 or ‘index’, 1 or ‘columns’, None}. The default value is None. |
Examples
1. Add prefix "item_" to the index labels of Series
In this example, we shall take a Series object in series with three string values, add the prefix "item_" to the labels of the Series object.
import pandas as pd
# Take a Series
series = pd.Series(["apple", "banana", "cherry"])
# Add a prefix to the labels
prefixed_series = series.add_prefix('item_')
# Print original and result Series
print("Original Series:")
print(series)
print("\nSeries after adding prefix:")
print(prefixed_series)Output
Original Series:
0 apple
1 banana
2 cherry
dtype: object
Series after adding prefix:
item_0 apple
item_1 banana
item_2 cherry
dtype: objectSummary
In this tutorial, we have seen about Series.add_prefix() method is a convenient way to add a prefix to the labels of a Pandas Series.