
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Suffix a String to Pandas Series Index Labels
The add_suffix is the panda Series function which is used to add a string suffix to the series index labels. this method will return a new series object with updated labels.
This add_suffic method takes a string as a parameter, and using that string will update the series labels. It will add the given string after the index labels of the series.
Example
# import pandas package import pandas as pd # create a pandas series s = pd.Series([2,4,6,8,10]) print(series) result = s.add_suffix('_Index') print("Resultant series with updated labels: ", result)
Explanation
In this following example, we created a series object ‘s’ using python list and we haven’t defined index labels for this series object. The pandas series constructor will automatically assign index values from 0 to 4.
By using this add_suffix method we can change the labels for our series object “s”.
Output
0 1 1 3 2 5 3 7 4 9 dtype: int64 Resultant series with updated labels: 0_Index 2 1_Index 4 2_Index 6 3_Index 8 4_Index 10 dtype: int64
The series with index representation 0-4 is the actual series, and The series which has indexes ending with “_Index” is the resultant series of s.add_suffix method.
Example
import pandas as pd sr = pd.Series({'A':3,'B':5,'C':1}) print(sr) # add suffix result = sr.add_suffix('_Lable') print('Resultant series with updated labels: ',result)
Explanation
The “_Label” is a string that is given as a parameter to the add_suffix function of the series object, and it will return a new pandas series object with new index labels.
Output
A 3 B 5 C 1 dtype: int64 Resultant series with updated labels: A_Lable 3 B_Lable 5 C_Lable 1 dtype: int64
This block has two pandas series objects, the first one is the actual series object and the last series object is the output of the add_suffix method. The add_suffix method has updated the index labels from A, B, C to A_Lable, B_Lable, C_Lable.