目标是迭代一个文件,找到前5名球队和球员的进攻篮板。在该文件中包含多个团队所玩的多个游戏。例如,Grizzlies vs Bears将参加比赛,而灰熊队的一名球员可能会在那场比赛中得到3个篮板,然后在灰熊队和鲨鱼队的比赛中输掉几百线,有些人可能会得到5个篮板。它的格式如下:' Off Rebound(1)' '关闭反弹(2)','关闭反弹(3)'等
目标是在遍历整个文件的循环中放置一个循环。我能够发现每场比赛的最多篮板数为10.而不是做一些非常丑陋的事情并且将10个if / else语句放在1-10中,我想添加一些变量(k)1-10以减少它邋's(我想抽出时间说我意识到代码不是很好。我今晚坐下来第一次看它,我对Python很新,我和#39;当我得到一个功能齐全的代码时,我会尽力修改它。也许这是一个糟糕的方法,但这就是我要做的事情)。不幸的是,我得到了这个非常奇怪的错误,我已经在我的代码下面粘贴了它。
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
答案 0 :(得分:0)
该行发生错误:
if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan':
这是一个关键错误,因此 eventDescript 或播放器字典中的 i 键没有任何价值。
这是因为您在while循环中使用!= 而不是&lt; ,并且增加 i 三次,以便 i 变得比你字典的长度更长。
以下是我将如何做到这一点:
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))