diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 23654613104ec..64787869ef46c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2963,15 +2963,56 @@ def _update_inplace(self, result, verify_is_copy=True): def add_prefix(self, prefix): """ - Concatenate prefix string with panel items names. + Prefix labels with string `prefix`. + + For Series, the row labels are prefixed. + For DataFrame, the column labels are prefixed. Parameters ---------- - prefix : string + prefix : str + The string to add before each label. Returns ------- - with_prefix : type of caller + Series or DataFrame + New Series or DataFrame with updated labels. + + See Also + -------- + Series.add_suffix: Suffix labels with string `suffix`. + + Examples + -------- + >>> s = pd.Series([1, 2, 3, 4]) + >>> s + 0 1 + 1 2 + 2 3 + 3 4 + dtype: int64 + + >>> s.add_prefix('item_') + item_0 1 + item_1 2 + item_2 3 + item_3 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_prefix('col_') + col_A col_B + 0 1 3 + 1 2 4 + 2 3 5 + 3 4 6 """ new_data = self._data.add_prefix(prefix) return self._constructor(new_data).__finalize__(self)