简体   繁体   English

无法在列表上使用索引

[英]Trouble using index on a list

I am trying to scrape odds from multiple sites but obviously, some sites use different names for different teams.我试图从多个站点中获取赔率,但很明显,有些站点对不同的团队使用不同的名称。 To still be able to handle my data efficiently I want to change my scraped data (team names in this case).为了仍然能够有效地处理我的数据,我想更改我的抓取数据(在这种情况下为团队名称)。 I have an excel file with all the team names per site for the premier league and one column with my preferred names.我有一个 excel 文件,其中包含英超联赛每个站点的所有球队名称,还有一个列有我喜欢的名称。 Now I am trying to change the scraped names, which I stored in 'List' for now, depending on whether they actually need to be changed.现在我正在尝试更改我现在存储在“列表”中的刮取名称,具体取决于它们是否确实需要更改。 If they need to be changed I want to change them to their corresponding name in the 'MAIN' column in my excel file.如果需要更改它们,我想在我的 excel 文件的“主要”列中将它们更改为相应的名称。

but when I try to change the name I can't find the corresponding correct name as 'Team_indice' is a list and not an integer.但是当我尝试更改名称时,我找不到相应的正确名称,因为“Team_indice”是一个列表而不是整数。

import pandas as pd


List = ['Brentford', 'Arsenal', 'Manchester United', 'Leeds United', 'Everton', 'Southampton', 'Burnley', 'Brighton & Hove Albion', 'Leicester City', 'Wolverhampton Wanderers', 'Chelsea', 'Crystal Palace', 'Watford', 'Aston Villa', 'Norwich City', 'Liverpool', 'Newcastle United', 'West Ham', 'Tottenham', 'Manchester City']
TeamNames = pd.read_excel(r'C:\Users\Axelz\PycharmProjects\ArbitrageBetting\Teams.xlsx', engine='openpyxl')
TeamNamesUnibet = TeamNames['Unibet'].tolist()
TeamNamesMain = TeamNames['MAIN'].tolist()

print(TeamNamesUnibet)
for Team in List:
    Team_indice = TeamNames.index[TeamNames['Unibet'] == Team].tolist()
    print(Team)

    if Team in TeamNamesMain:
        print('ok')
    else:
        print('needs change')
        Team = TeamNamesMain[Team_indice]
        print(Team)

NOTE I have tried the following:注意我尝试了以下方法:

Removing .tolist() but then the returned values become of the sort Int64Index, which I also can't convert to integers.删除.tolist()但随后返回的值变成了排序 Int64Index,我也无法将其转换为整数。 Simply trying int() but if you can answer my question you probably already know there is no way that is working :(简单地尝试int()但如果你能回答我的问题,你可能已经知道没有办法了:(

Try refactoring your code like this:尝试像这样重构你的代码:

import pandas as pd

teams = [
    "Brentford",
    "Arsenal",
    "Manchester United",
    "Leeds United",
    "Everton",
    "Southampton",
    "Burnley",
    "Brighton & Hove Albion",
    "Leicester City",
    "Wolverhampton Wanderers",
    "Chelsea",
    "Crystal Palace",
    "Watford",
    "Aston Villa",
    "Norwich City",
    "Liverpool",
    "Newcastle United",
    "West Ham",
    "Tottenham",
    "Manchester City",
]
TeamNames = pd.read_excel(
    r"C:\Users\Axelz\PycharmProjects\ArbitrageBetting\Teams.xlsx", engine="openpyxl"
)
TeamNamesUnibet = TeamNames["Unibet"].tolist()
TeamNamesMain = TeamNames["MAIN"].tolist()

print(TeamNamesUnibet)

for team in teams:
    print(team)
    team_indice = TeamNames[TeamNames["Unibet"] == team].index[0]

    if team in TeamNamesMain:
        print("ok")
    else:
        print("needs change")
        new_team = TeamNamesMain[team_indice]
        print(new_team)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM