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

后端 未结 2 1020
暗喜
暗喜 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:52

    You need assign list to summaryDF.index, if length of list is same as length of DataFrame:

    summaryDF.index = ['A','B','C', 'D','E','F','G','H','I','J','K','L']
    print (summaryDF)
       accuracy        f1  precision    recall
    A     0.494  0.722433   0.722433  0.722433
    B     0.290  0.826087   0.826087  0.826087
    C     0.274  0.629630   0.629630  0.629630
    D     0.278  0.628571   0.628571  0.628571
    E     0.288  0.718750   0.718750  0.718750
    F     0.740  0.740000   0.740000  0.740000
    G     0.698  0.765133   0.765133  0.765133
    H     0.582  0.778547   0.778547  0.778547
    I     0.682  0.748235   0.748235  0.748235
    J     0.574  0.767918   0.767918  0.767918
    K     0.398  0.711656   0.711656  0.711656
    L     0.530  0.780083   0.780083  0.780083
    
    print (summaryDF.index)
    Index(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L'], dtype='object')
    

    Timings:

    In [117]: %timeit summaryDF.index = ['A','B','C', 'D','E','F','G','H','I','J','K','L']
    The slowest run took 6.86 times longer than the fastest. This could mean that an intermediate result is being cached.
    10000 loops, best of 3: 76.2 µs per loop
    
    In [118]: %timeit summaryDF.set_index(pd.Index(['A','B','C', 'D','E','F','G','H','I','J','K','L']))
    The slowest run took 6.77 times longer than the fastest. This could mean that an intermediate result is being cached.
    1000 loops, best of 3: 227 µs per loop
    

    Another solution is convert list to numpy array:

    summaryDF.set_index(np.array(['A','B','C', 'D','E','F','G','H','I','J','K','L']), inplace=True)
    print (summaryDF)
       accuracy        f1  precision    recall
    A     0.494  0.722433   0.722433  0.722433
    B     0.290  0.826087   0.826087  0.826087
    C     0.274  0.629630   0.629630  0.629630
    D     0.278  0.628571   0.628571  0.628571
    E     0.288  0.718750   0.718750  0.718750
    F     0.740  0.740000   0.740000  0.740000
    G     0.698  0.765133   0.765133  0.765133
    H     0.582  0.778547   0.778547  0.778547
    I     0.682  0.748235   0.748235  0.748235
    J     0.574  0.767918   0.767918  0.767918
    K     0.398  0.711656   0.711656  0.711656
    L     0.530  0.780083   0.780083  0.780083
    

提交回复
热议问题