Python Forum
Referencing string names in df to outside variables - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Referencing string names in df to outside variables (/thread-35549.html)



Referencing string names in df to outside variables - illmattic - Nov-16-2021

I am trying to create a point system of NFL teams calculating points based on wins and losses relating to how many points they win by and the strength of their opponent.

I have the following df:
Output:
Date Visitor Home Visitor Score Home Score 11/14/2021 Minnesota Vikings Los Angeles Chargers 27 20 11/14/2021 Philadelphia Eagles Denver Broncos 30 13 11/14/2021 Seattle Seahawks Green Bay Packers 0 17 11/14/2021 Kansas City Chiefs Las Vegas Raiders 41 14 11/15/2021 Los Angeles Rams San Francisco 49ers 10 31
my current thinking about how to calculate the points according to how many points they win by is the following:
gb = 0

for game in range(len(nfl_df)):
    if nfl_df['Visitor'][game] == 'Green Bay Packers' and nfl_df['Visitor Score'][game] - nfl_df['Home Score'][game] < 9 and nfl_df['Visitor Score'][game] - nfl_df['Home Score'][game] > 0:
        gb += 3
    elif nfl_df['Visitor'][game] == 'Green Bay Packers' and nfl_df['Visitor Score'][game] - nfl_df['Home Score'][game] < 17 and nfl_df['Visitor Score'][game] - nfl_df['Home Score'][game] > 8:
        gb += 5
    elif nfl_df['Visitor'][game] == 'Green Bay Packers' and nfl_df['Visitor Score'][game] - nfl_df['Home Score'][game] > 16:
        gb += 7   
    else:
        pass
but if I were to do this with each team it would be a lot of code and I'm sure there is a simpler way to do it. But also, I don't know how to incorporate the strength of the opponent because variable 'gb' has no connection to the string value 'Green Bay Packers' which appears in the df. After each week I would like to have a team's points updated based on their win/loss for that week and incorporate that number into the win/loss calculation. For example, if Green Bay beats Dallas (a team with 50 points) I would have the points from the victory multiply by the total points the opponent has (3*50). I'm just stuck on how to reference the teams' names in the df ('Green Bay Packers', 'Dallas Cowboys') to their point variables (gb, dal)

Any help would be appreciated
Matt


RE: Referencing string names in df to outside variables - jefsummers - Nov-16-2021

You almost never want to loop through a dataframe. A lot of the point of dataframes is the ability to do things as a vector.
So, not using a loop (this is untested as I don't have your dataframe)
nfl_df['Home Wins'] = (nfl_df['Home Score'] > nfl_df['Visitor Score'])
nfl_df['Margin'] = abs(nfl_df['Home Score'] - nfl_df['Visitor Score'])
I would also just extract a set of the team names. Using a set eliminates duplicates