Pandas rename index

后端 未结 3 601
庸人自扰
庸人自扰 2020-12-15 14:32

I have the following dataframe, where I want to rename the index fromsummary to id:

summary  student  count 
0        error    6
1          


        
3条回答
  •  一生所求
    2020-12-15 15:07

    you need to access the index's properties

    df.index.name = 'id'
    

    original

             student  count
    summary               
    0         error      6
    1           yes      1
    2            no      1
    3         other      9
    

    fixed df:

        student  count
    id               
    0    error      6
    1      yes      1
    2       no      1
    3    other      9
    

    Update: seems like you had a name for the column's index. You should remove it with

    df.columns.names = ''

提交回复
热议问题