简体   繁体   English

使用串联的数据框时,为什么会出现“ IndexError:字符串索引超出范围”

[英]Why am I getting an ‘IndexError: string index out of range’ when I use a concatenated dataframe

So basically I use the same code in two separate cases. 所以基本上我在两种情况下使用相同的代码。

Case 1: I open and read a csv file using csv module and implement the code on the data taken from that csv file. 情况1:我使用csv模块打开并读取一个csv文件,并对从该csv文件获取的数据实施代码。 The code works fine in this case 在这种情况下,代码可以正常工作

Case 2: I use the same code but this time instead of using csv module I use pandas and glob in order to concat 2 csv files(same number of rows and columns) implement the code on the newly concatenated dataframe but get the error 'IndexError: string index out of range'. 情况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))

Here is the full error message: 这是完整的错误消息:

  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

Here is a sample from print(frame): 这是来自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```

For your case, please recheck your frame data. 对于您的情况,请重新检查您的frame数据。

You can check it by print out data: 您可以通过print出数据来检查它:

for game in frame:
    print(game, len(game))

Pretty sure that at least one game will have less than 4 characters inside. 可以肯定的是,至少一款game内的字符数少于4个。 Eg pyt , so when you call game[3] is string index out of range 例如pyt ,因此当您调用game[3]string index out of range

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么收到此错误“ IndexError:字符串索引超出范围” - Why am I getting this Error “IndexError: string index out of range” 为什么会出现 IndexError:字符串索引超出范围? - Why am I getting an IndexError: string index out of range? 为什么我收到 IndexError: list index out of range 这个函数? - Why am I getting IndexError: list index out of range for this function? 为什么我收到 IndexError: list index out of range - Why am I getting IndexError: list index out of range 为什么在此代码中出现IndexError:list index超出范围? - Why am I getting IndexError: list index out of range in this code? 为什么我看到“ IndexError:字符串索引超出范围” - Why am I seeing “IndexError: string index out of range” 当我的代码似乎在范围内时,为什么会出现 IndexError: list index out of range? - Why am I getting an IndexError: list index out of range when my code appears to be within the range? 为什么在云上训练时会收到“IndexError: list index out of range”? - Why am I getting "IndexError: list index out of range" when training on the cloud? 为什么在打印列表时没有出现错误时出现 IndexError: string index out of range? - why getting IndexError: string index out of range when i get no error when i print the list? 我收到IndexError:列表索引超出范围 - I am getting IndexError: list index out of range
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM