Pandas Series: add_suffix() function
Suffix labels with string suffix in Pandas series
The add_suffix() function is used to suffix labels with string suffix.
For Series, the row labels are suffixed. For DataFrame, the column labels are suffixed.
Syntax:
Series.add_suffix(self, suffix)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
suffix | The string to add after each label. | str | Required |
Returns: Series or DataFrame
New Series or DataFrame with updated labels
Example:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series([2, 3, 4, 5])
s
Output:
0 2 1 3 2 4 3 5 dtype: int64
Python-Pandas Code:
import numpy as np
import pandas as pd
s.add_suffix('_item')
df = pd.DataFrame({'X': [2, 3, 4, 5], 'Y': [4, 5, 6, 7]})
df
Output:
X Y 0 2 4 1 3 5 2 4 6 3 5 7
Python-Pandas Code:
import numpy as np
import pandas as pd
s.add_suffix('_item')
df = pd.DataFrame({'X': [2, 3, 4, 5], 'Y': [4, 5, 6, 7]})
df.add_suffix('_col')
Output:
X_col Y_col 0 2 4 1 3 5 2 4 6 3 5 7
Previous: Prefix labels with string prefix in Pandas series
Next: Subset rows or columns of Pandas dataframe
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/pandas/series/series-add_suffix.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics