Search code examples
pythonloopsdictionarykeyerror

Odd Python error probably due to looping


The goal is to iterate through a file and find the top 5 teams and players with respect to offensive rebounds. In the file contains multiple games played by multiple teams. For instance, Grizzlies vs Bears will play and someone from the Grizzlies might get 3 rebounds that game and then a couple hundred lines down the Grizzlies vs the Sharks and that some person might get 5 rebounds. It is formatted like this: 'Off Rebound (1)' 'Off Rebound (2)', 'Off Rebound (3)' etc.

The goal is to put a loop within the loop that iterates through the entire file. I was able to find out that the most rebounds per one game was 10. Rather than doing something really ugly and putting 10 if/else statements 1-10, I would like to add some variable (k) 1-10 to make it less sloppy than it is (which I'd like to take the time out to say I realize the code is not very good. I just sat down tonight to look at it for the first time, I am fairly new to Python and I'll revise it the best I can when I get a fully functional code.. maybe that's a bad way to do it, but that's what I will do). Unfortunately, I get this very odd error that I've pasted underneath my code here.

import pandas as pd
import collections as c

fileRead = pd.read_csv('Sample.csv')

eventDescript = fileRead.event_desc.apply(str)
team = fileRead.team_name.apply(str)
player = fileRead.player_name.apply(str)
topTeams = []
topPlayers = []
i = 0

while (i != len(eventDescript)):
   # print eventDescript.where(eventDescript == 'Off Rebound (1)').dropna()
   for k in range(1,11):
       if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan':
           topTeams.append(team[i])
           topPlayers.append(player[i])
           i = i + 1
       else:
           i = i + 1
   i = i + 1

print c.Counter(topTeams).most_common(5)
print c.Counter(topPlayers).most_common(5)

runfile('/Users/air13/Downloads/untitled2.py', wdir='/Users/air13/Downloads')

Error: Traceback (most recent call last):

  File "<ipython-input-239-f1cd8a80a240>", line 1, in <module>
    runfile('/Users/air13/Downloads/untitled2.py', wdir='/Users/air13/Downloads')
  File "/Users/air13/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "/Users/air13/anaconda/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)
  File "/Users/air13/Downloads/untitled2.py", line 24, in <module>
    if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan':
  File "/Users/air13/anaconda/lib/python2.7/site-packages/pandas/core/series.py", line 603, in __getitem__
    result = self.index.get_value(self, key)
  File "/Users/air13/anaconda/lib/python2.7/site-packages/pandas/indexes/base.py", line 2169, in get_value
    tz=getattr(series.dtype, 'tz', None))
  File "pandas/index.pyx", line 98, in pandas.index.IndexEngine.get_value (pandas/index.c:3557)
  File "pandas/index.pyx", line 106, in pandas.index.IndexEngine.get_value (pandas/index.c:3240)
  File "pandas/index.pyx", line 154, in pandas.index.IndexEngine.get_loc (pandas/index.c:4279)
  File "pandas/src/hashtable_class_helper.pxi", line 404, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8564)
  File "pandas/src/hashtable_class_helper.pxi", line 410, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:8508)
KeyError: 128651

Solution

  • The error occured in the line: if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan': It's a key error, so there is no value for the key i in your eventDescript or player dictionary. This is because you use an != instead of < in the while loop, and increase i three times, so that i became greater than the length of your dictionary.

    Here is how i would have done it:

    import pandas as pd
    import collections as c
    
    fileRead = pd.read_csv('Sample.csv')
    
    eventDescript = fileRead.event_desc.apply(str)
    team = fileRead.team_name.apply(str)
    player = fileRead.player_name.apply(str)
    topTeams = []
    topPlayers = []
    
    for i in range(len(eventDescript)):
        # print eventDescript.where(eventDescript == 'Off Rebound (1)').dropna()
        if player[i] != 'nan':
            k = int(eventDescript[i].replace('Off Rebound (', '').replace(')', ''))
            if 1 <= k <= 10:
                topTeams.append(team[i])
                topPlayers.append(player[i])
    
    print(c.Counter(topTeams).most_common(5))
    print(c.Counter(topPlayers).most_common(5))
    
    1. I recommend you to always use for-loops if possible, so these kind of errors can't even happen.
    2. You don't need the second loop.
    3. Instead of i = i + 1 you could have used i += 1