Search code examples
pythonstringpandasseries

Append suffix to a pandas series index


Given the following Panda series:

>>>series = pd.Series([1, 2, 3], index=["a", "b", "c"])
>>>series
a   1
b   2
c   3
dtype: int64

Is there a way to produce this?

>>>series.do_something()
a_x   1
b_x   2
c_x   3
dtype: int64

Background

I have a Series that was produced from a DataFrame aggregate function: df.sum()

The indexes are currently the column names, but I want them to be the column names plus _sum, like so:

>>>data
col1_sum   500.00
col2_sum   9324.0
col3_sum   0.2340
dtype: float64

Solution

  • In [49]: series.index += '_sum'
    
    In [50]: series
    Out[50]:
    a_sum    1
    b_sum    2
    c_sum    3
    dtype: int64