Can't set index of a pandas data frame - getting “KeyError”

后端 未结 2 1036
暗喜
暗喜 2021-01-04 21:21

I generate a data frame that looks like this (summaryDF):

   accuracy        f1  precision    recall
0     0.494  0.722433   0.722433  0.722433
         


        
2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-04 21:38

    I guess you and @jezrael misunderstood an example from the pandas docs:

    df.set_index(['A', 'B'])
    

    A and B are column names / labels in this example:

    In [55]: df = pd.DataFrame(np.random.randint(0, 10, (5,4)), columns=list('ABCD'))
    
    In [56]: df
    Out[56]:
       A  B  C  D
    0  6  9  7  4
    1  5  1  3  4
    2  4  4  0  5
    3  9  0  9  8
    4  6  4  5  7
    
    In [57]: df.set_index(['A','B'])
    Out[57]:
         C  D
    A B
    6 9  7  4
    5 1  3  4
    4 4  0  5
    9 0  9  8
    6 4  5  7
    

    The documentation says it should be list of column labels / arrays.

    so you were looking for:

    In [58]: df.set_index([['A','B','C','D','E']])
    Out[58]:
       A  B  C  D
    A  6  9  7  4
    B  5  1  3  4
    C  4  4  0  5
    D  9  0  9  8
    E  6  4  5  7
    

    but as @jezrael has suggested df.index = ['A','B',...] is faster and more idiomatic method...

提交回复
热议问题