Skip to content Skip to sidebar Skip to footer

Can't Set Index Of A Pandas Data Frame - Getting "keyerror"

I generate a data frame that looks like this (summaryDF): accuracy f1 precision recall 0 0.494 0.722433 0.722433 0.722433 0 0.290 0.826087 0.826087 0.

Solution 1:

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
0697415134244053909846457

In [57]: df.set_index(['A','B'])
Out[57]:
     C  D
A B
69745134440590986457

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]:
   ABCDA6974B5134C4405D9098E6457

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

Solution 2:

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.86times 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.77times 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)
   accuracyf1precisionrecallA0.4940.7224330.7224330.722433B0.2900.8260870.8260870.826087C0.2740.6296300.6296300.629630D0.2780.6285710.6285710.628571E0.2880.7187500.7187500.718750F0.7400.7400000.7400000.740000G0.6980.7651330.7651330.765133H0.5820.7785470.7785470.778547I0.6820.7482350.7482350.748235J0.5740.7679180.7679180.767918K0.3980.7116560.7116560.711656L0.5300.7800830.7800830.780083

Post a Comment for "Can't Set Index Of A Pandas Data Frame - Getting "keyerror""