Search code examples
pythonpandasscikit-learnsklearn-pandas

Extending the column name in pandas DataFrame


I have a data frame which contains 34 rows and 10 columns. I called the data frame "comp" now I did "invcomp = 1/comp", So the values changed but column name will be same. I want to replace or rename my column names, suppose the earlier name of my first column was "Cbm_m" in "comp", now I want to convert it to "Cbm_m_inv" in "invcomp". Extending or adding an extra term in last.


Solution

  • Use 'add_suffix':

    invcomp = invcomp.add_suffix('_inv')
    

    Setup:

    invcomp = pd.DataFrame(pd.np.random.rand(5,5), columns=list('ABCDE'))
    invcomp = invcomp.add_suffix('_inv')
    

    Output:

          A_inv     B_inv     C_inv     D_inv     E_inv
    0  0.111604  0.016181  0.384071  0.608118  0.944439
    1  0.523085  0.139200  0.495815  0.007926  0.183498
    2  0.090169  0.357117  0.381938  0.222261  0.788706
    3  0.802219  0.002049  0.173157  0.716345  0.182829
    4  0.260781  0.376730  0.646595  0.324361  0.345097