Skip to content
Related Articles

Related Articles

Python | Pandas Series.add_prefix()

View Discussion
Improve Article
Save Article
  • Last Updated : 20 Feb, 2019
View Discussion
Improve Article
Save Article

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.




# 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)


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.




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


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.




# 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)


Output :

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




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


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
Recommended Articles
Page :

Start Your Coding Journey Now!