0juronf
0juronf

Reputation: 55

How to run function through a list?

  teamname=print(prem['HomeTeam'].unique())

  ['Liverpool' 'West Ham' 'Bournemouth' 'Burnley' 'Crystal Palace' 'Watford' 'Tottenham' 'Leicester' 
  'Newcastle' 'Man United' 'Arsenal' 'Aston Villa' 'Brighton' 'Everton' 'Norwich' 'Southampton' 'Man 
   City' 'Sheffield United' 'Chelsea' 'Wolves']

   def TeamsPointsDict(df,teamname):
       team_name = teamname 
       num_points = df.loc[prem['HomeTeam'] == teamname, 'HP'].sum() + df.loc[prem['AwayTeam']==teamname, 'AP'].sum()
       d=dict()
       d[team_name]= num_points
       return d



       print(TeamsPointsDict(df,'Man City'))

       {'Man City': 57}

So I have created the list teamname above and then create a function that returns a dictionary with one name from the list teamname and the amount of points they have. Now, I am wondering how I would be able to run the function through the entire list of teamname and print all the teams and their respective points. Thank you :).

Upvotes: 0

Views: 71

Answers (2)

Parfait
Parfait

Reputation: 107587

Since your operation is essentially adding two sum aggregations, consider pandas.groupby with a join requiring no for loop:

def TeamsPointsDict(df, teamname):
    agg_df = (df.groupby(['HomeTeam'])['HP'].sum()
                .to_frame()
                .query("HomeTeam == @teamname")
                .join(df.groupby(['AwayTeam'])['AP'].sum())
                .sum(axis=1)
             )

    return agg_df.to_dict()

print(TeamsPointsDict(df, 'Man City'))

To demonstrate with random data:

import numpy as np
import pandas as pd

teams = ['Liverpool', 'West Ham', 'Bournemouth', 'Burnley', 'Crystal Palace', 
         'Watford', 'Tottenham', 'Leicester', 'Newcastle', 'Man United', 
         'Arsenal', 'Aston Villa', 'Brighton', 'Everton', 'Norwich', 
         'Southampton', 'Man City', 'Sheffield United', 'Chelsea', 'Wolves']

### DATA BUILD
np.random.seed(41320)
random_df = pd.DataFrame({'HomeTeam': np.random.choice(teams, 500),
                          'HP': np.random.randint(1, 10, 500),
                          'AwayTeam': np.random.choice(teams, 500),
                          'AP': np.random.randint(1, 10, 500)})

def TeamsPointsDict(df, teamname):
    agg_df = (df.groupby(['HomeTeam'])['HP'].sum()
                .to_frame()
                .query("HomeTeam == @teamname")
                .join(df.groupby(['AwayTeam'])['AP'].sum())
                .sum(axis=1)
             )

    return agg_df.to_dict()


print(TeamsPointsDict(random_df, 'Man City'))
# {'Man City': 238}

Upvotes: 0

QurakNerd
QurakNerd

Reputation: 804

I'm not sure I fully get what's happening in your code but,

Would this do what you want? If not comment on why not and I will add to the answer

for team in ARRAY_OF_TEAMS:
    print(TeamsPointsDict(df, team));

Upvotes: 2

Related Questions