奇怪的 Python 错误可能是由于循环
Odd Python error probably due to looping
目标是遍历文件并找到进攻篮板前 5 名的球队和球员。文件中包含多支球队进行的多场比赛。例如,灰熊队对熊队的比赛将会进行,灰熊队的某个人在那场比赛中可能会得到 3 个篮板,然后在灰熊队对大鲨鱼队的比赛中,有人可能会得到 5 个篮板。其格式如下:'Off Rebound (1)' 'Off Rebound (2)'、'Off Rebound (3)' 等
目标是在遍历整个文件的循环中放置一个循环。我能够发现每场比赛最多的篮板是 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
行中出现错误:
if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan':
这是一个键错误,因此 i 键在 eventDescript 或 player 字典中没有值.
这是因为你在 while 循环中使用了 != 而不是 <,并且增加了 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))
- 我建议您尽可能始终使用 for 循环,这样此类错误就不会发生。
- 您不需要第二个循环。
- 您可以使用 i += 1 而不是 i = i + 1
目标是遍历文件并找到进攻篮板前 5 名的球队和球员。文件中包含多支球队进行的多场比赛。例如,灰熊队对熊队的比赛将会进行,灰熊队的某个人在那场比赛中可能会得到 3 个篮板,然后在灰熊队对大鲨鱼队的比赛中,有人可能会得到 5 个篮板。其格式如下:'Off Rebound (1)' 'Off Rebound (2)'、'Off Rebound (3)' 等
目标是在遍历整个文件的循环中放置一个循环。我能够发现每场比赛最多的篮板是 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
行中出现错误:
if eventDescript[i] == 'Off Rebound (' + str(k) + ')' and player[i] != 'nan':
这是一个键错误,因此 i 键在 eventDescript 或 player 字典中没有值.
这是因为你在 while 循环中使用了 != 而不是 <,并且增加了 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))
- 我建议您尽可能始终使用 for 循环,这样此类错误就不会发生。
- 您不需要第二个循环。
- 您可以使用 i += 1 而不是 i = i + 1