简体   繁体   中英

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. 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.

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.

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. Simply trying int() but if you can answer my question you probably already know there is no way that is working :(

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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