Python Forum
Referencing string names in df to outside variables
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Referencing string names in df to outside variables
#1
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
Reply
#2
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Variables in a SCPI string thomaswfirth 3 931 May-22-2023, 05:04 PM
Last Post: thomaswfirth
  name 'lblstatus' is not defined when referencing a label KatManDEW 4 1,539 Apr-21-2022, 12:33 PM
Last Post: KatManDEW
  Dictionary Referencing nickdavis2017 1 1,613 Nov-20-2021, 06:24 PM
Last Post: deanhystad
  Referencing a fixed cell Mark17 2 2,072 Dec-17-2020, 07:14 PM
Last Post: Mark17
Sad need help in referencing a list n00bdev 2 1,855 Nov-01-2020, 12:06 PM
Last Post: buran
  Issue referencing new instance from other class nanok66 3 2,239 Jul-31-2020, 02:07 AM
Last Post: nanok66
  referencing another method in a class Skaperen 6 2,670 Jul-02-2020, 04:30 AM
Last Post: Skaperen
  Theory behind referencing a dictionary rather than copying it to a list sShadowSerpent 2 2,092 Mar-24-2020, 07:18 PM
Last Post: sShadowSerpent
  Creating local variables from a string peckjonk 2 2,178 Feb-15-2020, 06:07 PM
Last Post: ndc85430
  "not defined" error in function referencing a class Exsul 2 3,686 Mar-27-2019, 11:59 PM
Last Post: Exsul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020