I’ll preface that I am a noobie working on a little sports analytics project. I’m trying to take NHL player statistics from an API and I’m having difficulty accessing the values of the dictionary to pull specific metrics. I am able to load the API properly and access the gamelog but cannot find a way to extract the values from the gamelog.
Here is what the dictionary looks like:
"gameLog": [
{
"gameId": 2023021306,
"teamAbbrev": "EDM",
"homeRoadFlag": "R",
"gameDate": "2024-04-17",
"goals": 0,
"assists": 0,
"commonName": {
"default": "Oilers"
},
"opponentCommonName": {
"default": "Coyotes"
},
"points": 0,
"plusMinus": -2,
"powerPlayGoals": 0,
"powerPlayPoints": 0,
"gameWinningGoals": 0,
"otGoals": 0,
"shots": 2,
"shifts": 20,
"shorthandedGoals": 0,
"shorthandedPoints": 0,
"pim": 0,
"toi": "17:44",
"opponentAbbrev": "ARI"
},
With multiple keys like this for each game played. I am trying to access specific metrics like ‘goals’, ‘points’, etc. sum them up and divide them to get averages.
I have tried:
for key in player_data_json['gameLog']:
goals = 0
for val in key:
if key == 'goals':
goals += key[val]
print(goals)
But returns zeroes every time.
Any help is greatly appreciated!
Nick Tetzlaff is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.