How can I change a specific row label in a Pandas dataframe?

前端 未结 2 705
余生分开走
余生分开走 2020-12-18 22:11

I have a dataframe such as:

      0     1    2    3    4    5
0  41.0  22.0  9.0  4.0  2.0  1.0
1   6.0   1.0  2.0  1.0  1.0  1.0
2   4.0   2.0  4.0  1.0  0.         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-18 22:42

    You can get the last index using negative indexing similar to that in Python

    last = df.index[-1]
    

    Then

    df = df.rename(index={last: 'a'})
    

    Edit: If you are looking for a one-liner,

    df.index = df.index[:-1].tolist() + ['a']
    

提交回复
热议问题