- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to 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.
- Related Articles
- How to prefix string to a pandas series labels?
- How to specify an index while creating a Series in Pandas?
- How to change index values of a Pandas series after creation?
- How to select subset of data with Index Labels in Python Pandas?
- How to get the index and values of series in Pandas?
- How to concrete a single Series into a string Python Pandas library?
- How to sort a Pandas Series?
- How to retrieve the last valid index from a series object using pandas series.last_valid_index() method?
- Write a Python code to concatenate two Pandas series into a single series without repeating the index
- How to append elements to a Pandas series?
- How to check if a string ends with a specified Suffix string in Golang?
- How to select subset of data with Index Labels?
- How to append a pandas Series object to another Series in Python?
- Python Pandas - Make new Index with passed list of labels deleted
- How to make a multi-index in Pandas?
