所以基本上我在两种情况下使用相同的代码。
情况1:我使用csv模块打开并读取一个csv文件,并对从该csv文件获取的数据实施代码。在这种情况下,代码可以正常工作
案例2:我使用相同的代码,但是这次不是使用csv模块,而是使用pandas和glob来连接2个csv文件(相同的行和列数),在新连接的数据帧上实现代码,但得到错误“ IndexError:字符串索引超出范围”。
import pandas as pd
import glob
path = r’Q:\Users\Panagiotis\Betting formula\EPL Seasons’
all_files = glob.glob(path + “/*.csv”)
li = []
for filename in all_files:
df = pd.read_csv(filename, index_col=None, header=0)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
upsets = 0
non_upsets = 0
starting_bankroll = 100
wagering_size = 5
bankroll = starting_bankroll
for game in frame:
home_team = game[2]
away_team = game[3]
home_goals = int(game[4])
away_goals = int(game[5])
home_odds = float(game[23])
draw_odds = float(game[24])
away_odds = float(game[25])
if home_odds > away_odds:
if home_goals > away_goals:
upsets += 1
bankroll += wagering_size * (home_odds - 1)
else:
non_upsets += 1
bankroll -= wagering_size
ROI = ((bankroll - starting_bankroll) / (wagering_size * (upsets +
non_upsets))) * 100
print (“There were ‘%s’ upsets out of ‘%s’ total matches” % (upsets,
upsets + non_upsets))
print (“Starting bankroll = ‘%s’” % (starting_bankroll))
print (“Finishing bankroll = ‘%s’ | ROI = ‘%s’” % (bankroll, ROI))
这是完整的错误消息:
File "<ipython-input-1-27ce3bbe0a95>", line 1, in <module>
runfile('Q:/Users/Panagiotis/Betting formula/csv concatenation formula.py', wdir='Q:/Users/Panagiotis/Betting formula')
File "Q:\Users\Panagiotis\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
execfile(filename, namespace)
File "Q:\Users\Panagiotis\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "Q:/Users/Panagiotis/Betting formula/csv concatenation formula.py", line 27, in <module>
away_team = game[3]
IndexError: string index out of range
这是来自print(frame)的样本:
Div Date HomeTeam AwayTeam ... BbAvAHA PSCH PSCD PSCA
0 E0 13-08-16 Burnley Swansea ... 1.81 2.79 3.16 2.89
1 E0 13-08-16 Crystal Palace West Brom ... 1.85 2.25 3.15 3.86```
答案 0 :(得分:0)
对于您的情况,请重新检查您的frame
数据。
您可以通过print
取出数据进行检查:
for game in frame:
print(game, len(game))
请确保至少一个game
的内部少于4个字符。例如。 pyt
,因此当您致电game[3]
时是string index out of range