2017-03-18 71 views
0

目标是遍历一个文件,并找到关于进攻篮板的前5名球队和球员。在文件中包含多个团队玩的游戏。例如,灰熊vs熊队将会出场,而来自灰熊队的人可能会拿到3个篮板球,然后在灰熊队和鲨鱼队之间排上几百个篮板球,而某些人可能会拿到5个篮板球。它的格式如下:'Off Rebound(1)''Off Rebound(2)','Off Rebound(3)'等。奇怪的Python错误可能是由于循环造成的

目标是在遍历整个文件的循环中放置一个循环。我能够发现每场比赛篮板数最多的是10次。而不是做一些非常丑陋的事情,并且把10个if/else的陈述1-10,我想添加一些变量(k)1-10来减少它(我想抽出时间说我意识到代码不是很好,我今晚就坐下来第一次看它,我对Python很新,我会当我得到全功能的代码时,尽可能地修改它..也许这是一个不好的方法来做到这一点,但这就是我将要做的)。不幸的是,我得到了这个我在代码下面粘贴的非常奇怪的错误。

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') 

错误: 回溯(最近通话最后一个):

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 

回答

1

错误的线路上出现: if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan': 这是一个关键的错误,所以对于关键没有价值的我在您的事件描述播放器字典。 这是因为您使用的!=而不是在while循环<,并增加三次,让我成为比你的字典的长度。

下面是我会做:

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. 我建议你总是使用for循环如果可能的话,那么这样的错误甚至不能发生。
  2. 你不需要第二个循环。
  3. 而不是i = i + 1你可以使用i + = 1
+0

我很感谢帮助,见过! – Tyler