Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2978,15 +2978,54 @@ def add_prefix(self, prefix):

def add_suffix(self, suffix):
"""
Concatenate suffix string with panel items names.
Suffix labels with string `suffix`.

For Series, the row labels are suffixed.
For DataFrame, the column labels are suffixed.

Parameters
----------
suffix : string
suffix : str
The string to add after each label.

Returns
-------
with_suffix : type of caller
Series or DataFrame
New Series or DataFrame with updated labels.

See Also
--------
Series.add_prefix: Prefix labels with string `prefix`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's for both Series and DataFrame would make sense to have the .add_prefix method of both classes. Or none of them, there is some discussion about whether to have them or not here: #20289 (comment)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Added in the latest commit to both.


Examples
--------
>>> s = pd.Series([1, 2, 3, 4])
>>> s
0 1
1 2
2 3
3 4
dtype: int64
>>> s.add_suffix('_item')
0_item 1
1_item 2
2_item 3
3_item 4
dtype: int64

>>> df = pd.DataFrame({'A': [1, 2, 3, 4], 'B': [3, 4, 5, 6]})
>>> df
A B
0 1 3
1 2 4
2 3 5
3 4 6
>>> df.add_suffix('_col')
A_col B_col
0 1 3
1 2 4
2 3 5
3 4 6
"""
new_data = self._data.add_suffix(suffix)
return self._constructor(new_data).__finalize__(self)
Expand Down