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

前端 未结 2 692
余生分开走
余生分开走 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:24

    use index attribute:

     df.index = df.index[:-1].append(pd.Index(['A']))
    
    0 讨论(0)
  • 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']
    
    0 讨论(0)
提交回复
热议问题