Python | Pandas Series.add_prefix()

Python | Pandas Series.add_prefix()

The add_prefix() method of a pandas Series is used to add a prefix string to the index labels of the Series. It is helpful when you want to modify the labels of the index with a common prefix.

Here's how you can use the add_prefix() method:

  • First, make sure you have pandas installed:
pip install pandas 
  • Here's a simple script to demonstrate the usage of add_prefix() on a pandas Series:
import pandas as pd # Create a sample pandas Series data = pd.Series([10, 20, 30, 40, 50], index=['a', 'b', 'c', 'd', 'e']) print("Original Series:") print(data) # Add prefix to the index labels prefixed_data = data.add_prefix('label_') print("\nSeries after adding prefix:") print(prefixed_data) 

Output:

Original Series: a 10 b 20 c 30 d 40 e 50 dtype: int64 Series after adding prefix: label_a 10 label_b 20 label_c 30 label_d 40 label_e 50 dtype: int64 

In the above example, the add_prefix() method added the prefix 'label_' to each of the index labels of the Series.


More Tags

gsutil administration ssrs-2012 key thumbnails ocr perforce swift amazon-ecs jql

More Programming Guides

Other Guides

More Programming Examples