Pandas Series add_suffix() Method
If we want to alter the index names of the Series, we can alter them by adding the suffix to the Series index using the Series.add_suffix()
method. This method adds a string label at the end
of each index or row and returns a Series with the updated labels.
The below is the syntax of the Series.add_suffix()
method.
Syntax
Series.add_suffix(suffix)
Parameters
prefix: This represents the str that is the string to be added after each label.
Example: Suffix labels of Series in Pandas
Let's create a Series
and add the string label at the end
of the row labels of the Series using the Series.add_suffix()
method. See the below example.
The Series.add_suffix()
method prefixes the string '_index'
to the rows of the Series.
#importing pandas as pd
import pandas as pd
#creating Series
s= pd.Series([1,2,3])
print("---------The series is------")
print(s)
print("-------After updating, the new series is---------")
print(s.add_suffix('_index'))
---------The series is------
0 1
1 2
2 3
dtype: int64
-------After updating, the new series is---------
0_index 1
1_index 2
2_index 3
dtype: int64
Example 2: Suffix labels of Series in Pandas
This example is similar to the previous one. Let's create a Series
and add the string label at the end
of the row labels of the Series
using the Series.add_suffix()
method. See the below example.
The Series.add_suffix()
method prefixes the string '_Student'
to the rows of the Series
.
#importing pandas as pd
import pandas as pd
#creating Series
s= pd.Series(['Navya','Vindya','Sinchana'],index=['First','Second','Third'])
print("---------The series is------")
print(s)
print("-------After updating, the new series is---------")
print(s.add_suffix('_Student'))
---------The series is------
First Navya
Second Vindya
Third Sinchana
dtype: object
-------After updating, the new series is---------
First_Student Navya
Second_Student Vindya
Third_Student Sinchana
dtype: object
Conclusion
In this tutorial, we learned the Python pandas Series.add_suffix()
method. We learned syntax and solved examples by applying this method to series.