Python | Pandas Series.add_prefix()

Pandas series is a One-dimensional ndarray with axis labels. The labels need not be unique but must be a hashable type. The object supports both integer- and label-based indexing and provides a host of methods for performing operations involving the index.

Pandas Series.add_prefix() function is used to add prefix to each index labels of the given series object.

Syntax: Series.add_prefix(prefix)



Parameter :
prefix : The string to add before each label.

Returns : Series or DataFrame

Example #1: Use Series.add_prefix() function to add prefix to each index labels in the given series object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([34, 5, 13, 32, 4, 15])
  
# Create the Index
index_ = ['Coca Cola', 'Sprite', 'Coke', 'Fanta', 'Dew', 'ThumbsUp']
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

chevron_right


Output :

Now we will use Series.add_prefix() function to add the string label ‘IPL 2019_’ before each index labels in the given series object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# add 'IPL 2019_' before each index labels
result = sr.add_prefix(prefix = 'IPL 2019_')
  
# Print the result
print(result)

chevron_right


Output :

As we can see in the output, the Series.add_prefix() function has successfully added the passed string label before each index labels in the given series object.
 
Example #2 : Use Series.add_prefix() function to add prefix to each index labels in the given series object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing pandas as pd
import pandas as pd
  
# Creating the Series
sr = pd.Series([51, 10, 24, 18, 1, 84, 12, 10, 5, 24, 0])
  
# Create the Index
# apply yearly frequency
index_ = pd.date_range('2010-10-09 08:45', periods = 11, freq ='Y')
  
# set the index
sr.index = index_
  
# Print the series
print(sr)

chevron_right


Output :

Now we will use Series.add_prefix() function to add the string label ‘Date_’ before each index labels in the given series object.

filter_none

edit
close

play_arrow

link
brightness_4
code

# add 'Date_' before each index labels
result = sr.add_prefix(prefix = 'Date_')
  
# Print the result
print(result)

chevron_right


Output :

As we can see in the output, the Series.add_prefix() function has successfully added the passed string label before each index labels in the given series object.



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.