Pandas rename index

后端 未结 3 600
庸人自扰
庸人自扰 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 = ''

    0 讨论(0)
  • 2020-12-15 15:13

    First you can drop the column:

    df = df.drop('summary', axis=1)
    df['id'] = np.arange(df.shape[0])
    df.set_index('id', inplace=True)
    

    Then you can get the desired result.

    0 讨论(0)
  • 2020-12-15 15:23

    You need to remove the column name:

    df.rename_axis(None, axis=1).rename_axis('id', axis=0)
    ##if pd.__version__ == 0.24.0 
    #df.rename_axis([None], axis=1).rename_axis('id')
    

    The problem is that 'summary' is your column name. When there is no index name, the column name is placed directly above the index, which can be misleading:

    import pandas as pd
    df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
    df.columns.name = 'col_name'
    print(df)
    
    #col_name  A  B
    #0         1  1
    #1         1  1
    #2         1  1
    #3         1  1
    

    When you then try to add an index name, it becomes clear that 'col_name' was really the column name.

    df.index.name = 'idx_name'
    print(df)
    
    #col_name  A  B
    #idx_name      
    #0         1  1
    #1         1  1
    #2         1  1
    #3         1  1
    

    There is no ambiguity though: when you have an index name, the columns are raised one level, which allows you to distinguish between an index name and a column name.

    df = pd.DataFrame([[1]*2]*4, columns=['A', 'B'])
    df.index.name = 'idx_name'
    print(df)
    
    #          A  B
    #idx_name      
    #0         1  1
    #1         1  1
    #2         1  1
    #3         1  1
    
    0 讨论(0)
提交回复
热议问题