Home > Back-end >  How can I fix this KeyError: (1, 0) in Python?
How can I fix this KeyError: (1, 0) in Python?

Time:09-27

My code is as follows:

stats = ['ETT', 'EMT', 'MRR', 'MMR', 'EER', 'ATT', 'MMT' ,'EMM' ,'ERT' ,'AER' ,'AEE' ,'AMM' ,'EEM' ,'ART' ,'AAR' ,'AEM' ,'MRT','AAT','AMR','ARR','AMT','RTT','EMR','ERR','EET','RRT','AET','MTT','AAE','AAM','EEE','TTT',
'AAA','MMM','RRR']


def determine_rewards(number_of_look_up_table,current_row_index,action_index):
    if stats[number_of_look_up_table][current_row_index] == 'T' :
        reward = rewards[0,action_index]
    elif stats[number_of_look_up_table][current_row_index] == 'M' :
        reward = rewards[1,action_index]
    elif stats[number_of_look_up_table][current_row_index] == 'E' :
        reward = rewards[2,action_index]   
    elif stats[number_of_look_up_table][current_row_index] == 'R' :
        reward = rewards[3,action_index] 
    elif stats[number_of_look_up_table][current_row_index] == 'A' :
        reward = rewards[4,action_index]    
    return reward   

reward = determine_rewards(2,0,0)

'rewards' is a matrix that has 5 rows and 20 columns and there are numbers inside it.The 'determine_rewards' function allocates a value using the inputs and values in the rewards and stats matrix. When I call function 'determine_rewards', it gives me the following error. Please help me to fix this error

reward = determine_rewards(2,0,0)


KeyError                                  Traceback (most recent call last)
~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, 
tolerance)
 2656             try:
-> 2657                 return self._engine.get_loc(key)
 2658             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: (1, 0)

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_28528/3264022016.py in <module>
----> 1 reward = determine_rewards(2,0,0)

~\AppData\Local\Temp/ipykernel_28528/255015173.py in 
determine_rewards(number_of_look_up_table, current_row_index, action_index)
 20         reward = rewards[0,action_index]
 21     elif stats[number_of_look_up_table][current_row_index] == 'M' :
 ---> 22         reward = rewards[1,action_index]
      23     elif stats[number_of_look_up_table][current_row_index] == 'E' :
      24         reward = rewards[2,action_index]

~\Anaconda3\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)
2925             if self.columns.nlevels > 1:
2926                 return self._getitem_multilevel(key)
-> 2927             indexer = self.columns.get_loc(key)
   2928             if is_integer(indexer):
   2929                 indexer = [indexer]

  ~\Anaconda3\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, 
  tolerance)
  2657                 return self._engine.get_loc(key)
  2658             except KeyError:
  -> 2659                 return self._engine.get_loc(self._maybe_cast_indexer(key))
  2660         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
  2661         if indexer.ndim > 1 or indexer.size > 1:

  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

  pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

  pandas/_libs/hashtable_class_helper.pxi in 
  pandas._libs.hashtable.PyObjectHashTable.get_item()

  pandas/_libs/hashtable_class_helper.pxi in 
  pandas._libs.hashtable.PyObjectHashTable.get_item()

  KeyError: (1, 0)

CodePudding user response:

Check indentation

Also I believe that maybe you messed up your square brackets?:

rewards[0,action_index]  -> rewards[0][action_index]

do the same for all cases

  • Related