VOOZH about

URL: https://www.geeksforgeeks.org/pandas/python-pandas-series-add_prefix/

⇱ Python | Pandas Series.add_prefix() - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python | Pandas Series.add_prefix()

Last Updated : 10 Apr, 2024

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.

Output :

Coca Cola 34
Sprite 5
Coke 13
Fanta 32
Dew 4
ThumbsUp 15
dtype: int64

Now we will use

Series.add_prefix()

function to add the string label 'IPL 2019_' before each index labels in the given series object.

Output :

IPL 2019_Coca Cola 34
IPL 2019_Sprite 5
IPL 2019_Coke 13
IPL 2019_Fanta 32
IPL 2019_Dew 4
IPL 2019_ThumbsUp 15
dtype: int64

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.

Output :

2010-12-31 08:45:00 51
2011-12-31 08:45:00 10
2012-12-31 08:45:00 24
2013-12-31 08:45:00 18
2014-12-31 08:45:00 1
2015-12-31 08:45:00 84
2016-12-31 08:45:00 12
2017-12-31 08:45:00 10
2018-12-31 08:45:00 5
2019-12-31 08:45:00 24
2020-12-31 08:45:00 0
Freq: A-DEC, dtype: int64

Now we will use

Series.add_prefix()

function to add the string label 'Date_' before each index labels in the given series object.

Output :

Date_2010-12-31 08:45:00 51
Date_2011-12-31 08:45:00 10
Date_2012-12-31 08:45:00 24
Date_2013-12-31 08:45:00 18
Date_2014-12-31 08:45:00 1
Date_2015-12-31 08:45:00 84
Date_2016-12-31 08:45:00 12
Date_2017-12-31 08:45:00 10
Date_2018-12-31 08:45:00 5
Date_2019-12-31 08:45:00 24
Date_2020-12-31 08:45:00 0
dtype: int64

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.

Comment

Explore