多级Dict中的键错误,即使键在Dict中

2024-10-03 06:28:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我的最终目标是从多级dictas seen in this tutorial创建一个pandas DataFrame。但是,我收到一个KeyError,指出其中一个键不在dict中。下面是多级dict的一个子集:

{
    "draftDetail": {
        "drafted": True,
        "inProgress": False
    },
    "gameId": 1,
    "id": 862068,
    "schedule": [
        {
            "away": {
                "adjustment": 0.0,
                "cumulativeScore": {
                    "losses": 0,
                    "statBySlot": NULL,
                    "ties": 0,
                    "wins": 0
                },
                "pointsByScoringPeriod": {
                    "1": 126.82
                },
                "teamId": 1,
                "tiebreak": 0.0,
                "totalPoints": 126.82
            },
            "home": {
                "adjustment": 0.0,
                "cumulativeScore": {
                    "losses": 0,
                    "statBySlot": NULL,
                    "ties": 0,
                    "wins": 0
                },
                "pointsByScoringPeriod": {
                    "1": 115.52
                },
                "teamId": 15,
                "tiebreak": 0.0,
                "totalPoints": 115.52
            },
            "id": 0,
            "matchupPeriodId": 1,
            "playoffTierType": "NULL",
            "winner": "AWAY"
        },
        {
            "away": {
                "adjustment": 0.0,
                "cumulativeScore": {
                    "losses": 0,
                    "statBySlot": NULL,
                    "ties": 0,
                    "wins": 0
                },
                "pointsByScoringPeriod": {
                    "1": 183.4
                },
                "teamId": 16,
                "tiebreak": 0.0,
                "totalPoints": 183.4
            },
            "home": {
                "adjustment": 0.0,
                "cumulativeScore": {
                    "losses": 0,
                    "statBySlot": NULL,
                    "ties": 0,
                    "wins": 0
                },
                "pointsByScoringPeriod": {
                    "1": 115.08
                },
                "teamId": 6,
                "tiebreak": 0.0,
                "totalPoints": 115.08
            },
            "id": 1,
            "matchupPeriodId": 1,
            "playoffTierType": "NULL",
            "winner": "AWAY"
        }
    ]
}

然后,要创建df,我使用以下代码:

df = [[
        game['matchupPeriodId'],
        game['home']['teamId'], game['home']['totalPoints'],
        game['away']['teamId'], game['away']['totalPoints']
    ] for game in d['schedule']] 
df = pd.DataFrame(df, columns=['Week', 'Team1', 'Score1', 'Team2', 'Score2', 'PlayoffTier'])
df.head()

但是,我得到了这个错误:

Traceback (most recent call last):

  File "<ipython-input-65-adda3411722d>", line 5, in <module>
    ] for game in d['schedule']] 

  File "<ipython-input-65-adda3411722d>", line 5, in <listcomp>
    ] for game in d['schedule']] 

KeyError: 'away'

我还尝试查看是否可以使用以下方法在dict中识别钥匙:

if 'away' in d['schedule']:
    print('will execute')
else:
    print('wont execute')

它不会执行

有没有关于如何修复错误的建议?为了进一步了解上下文,我连接到ESPN的FantasyFootball API以最初检索数据,并可以共享错误之前的代码

提前谢谢


Tags: ingamedfnullschedulelosseswinsaway
1条回答
网友
1楼 · 发布于 2024-10-03 06:28:53

当没有“离开”键时,您必须处理要做的事情。这是因为季后赛中有两支球队需要告别:

import requests
import pprint
import pandas as pd


#set the leagueID and year info for ESPN league:

league_id = 123456 (example to not give out private league info)
year = 2019

#set the url with params and cookies:

url = "https://fantasy.espn.com/apis/v3/games/ffl/leagueHistory/" + \
      str(league_id) + "?seasonId=" + str(year)

SWID = "{BF2E3A9D-093E-425E-B4A2-7DD5D4ABCAB1}"

Cookies = "AEB8LR5rfxLehSJOeku1TOugWAJfebsbk%2F5wBfrldGZ5svoy8Au1Ic%2BZX6P1y%2BWMcScgyoyuwvoRDv%2FqDfIAQRnjd9amDBSdk4Nze4hdTlTUFAa7Y9QJL4IAY0YNtrSPlFjVDGRugXRn309EpQeVWj5akI75GQjx%2FoGLtQ30UHzEdk4A8qBuxqjJ3oy9eIDPYOooncwVJ0AxbAAwuhSpqoEZOHwn8XAoCKyp4yU6H5HvyEGBUU9DiSHV6nvjLCZahWSAfd3TaY%2FMDQUutdaK5HcC"

#make request to API
r = requests.get(url, params={"view":"mMatchupScore"}, cookies={"swid":SWID, "espn_2": Cookies})
#set request as JSON
d = r.json()[0]
#explore JSON formatting
print(d)
#following the tutorial, create dataframe from MMatchScore end point:



#Option 1: insert the away key with teamId and totalPoints values of null

import numpy as np
for each in d['schedule']:
    if 'away' not in each.keys():
        each.update({'away': {'teamId':np.nan, 'totalPoints':np.nan}})

df = [[
        game['matchupPeriodId'],
        game['home']['teamId'], game['home']['totalPoints'],
        game['away']['teamId'], game['away']['totalPoints'],
        game['playoffTierType']
    ] for game in d['schedule']] #returning keyerror: 'away'

df = pd.DataFrame(df, columns=['Week', 'Team1', 'Score1', 'Team2', 'Score2', 'PlayoffTier'])
df['Type'] = ['Regular' if w<14 else 'Playoff' for w in df['Week']]
df.head()

df.shape


# Option 2: use a regular for loop in stead of list comprehension to deal with 
# if "away" not in key

weekList = []
team1List = []
score1List = []
team2List = []
score2List = []
playoffTierList = []

for game in d['schedule']:
    playoffTierList.append(game['playoffTierType'])
    weekList.append(game['matchupPeriodId'])
    team1List.append(game['home']['teamId'])
    score1List.append(game['home']['totalPoints'])
    
    if 'away' not in game.keys():
        team2List.append(np.nan)
        score2List.append(np.nan)
    else:
        team2List.append(game['away']['teamId'])
        score2List.append(game['away']['totalPoints'])
        
    
df = pd.DataFrame({'Week':weekList, 'Team1':team1List, 'Score1': score1List, 
                   'Team2':team2List, 'Score2':score2List, 'PlayoffTier':playoffTierList})
df['Type'] = ['Regular' if w<14 else 'Playoff' for w in df['Week']]
df.head()

df.shape        

输出:

print (df)
    Week  Team1  Score1  Team2  Score2                 PlayoffTier     Type
0      1     15  115.52    1.0  126.82                        NONE  Regular
1      1      6  115.08   16.0  183.40                        NONE  Regular
2      1      2  155.32    5.0  129.60                        NONE  Regular
3      1     17  133.86   13.0  109.04                        NONE  Regular
4      1     11  107.26    7.0  133.02                        NONE  Regular
5      1     12  108.14    8.0   66.26                        NONE  Regular
6      2      1  106.66   16.0  157.06                        NONE  Regular
7      2      5  102.80   15.0  119.56                        NONE  Regular
8      2      2  116.38    6.0  121.72                        NONE  Regular
9      2     13  104.76    7.0  111.52                        NONE  Regular
10     2      8  104.30   17.0  118.30                        NONE  Regular
11     2     12   83.62   11.0  104.02                        NONE  Regular
12     3      5   92.20    1.0  131.20                        NONE  Regular
13     3     16  162.54    2.0  116.48                        NONE  Regular
14     3     15  153.64    6.0  131.22                        NONE  Regular
15     3      8  114.84   13.0  113.34                        NONE  Regular
16     3      7  141.06   12.0  114.02                        NONE  Regular
17     3     17  123.36   11.0  123.32                        NONE  Regular
18     4      1  122.58    2.0  142.18                        NONE  Regular
19     4      6   96.84    5.0  117.10                        NONE  Regular
20     4     15   92.70   16.0  137.72                        NONE  Regular
21     4     13   98.10   12.0  124.38                        NONE  Regular
22     4     11   84.20    8.0  153.10                        NONE  Regular
23     4     17   62.78    7.0  108.10                        NONE  Regular
24     5      6  116.24    1.0  139.14                        NONE  Regular
25     5      2  119.74   15.0  193.54                        NONE  Regular
26     5      5  158.88   16.0  115.62                        NONE  Regular
27     5     11   82.04   13.0   92.62                        NONE  Regular
28     5     12  139.00   17.0  121.90                        NONE  Regular
29     5      8  118.92    7.0  130.14                        NONE  Regular
..   ...    ...     ...    ...     ...                         ...      ...
67    12     17   51.14   15.0  123.02                        NONE  Regular
68    12      7  125.68   16.0  119.52                        NONE  Regular
69    12      8   96.00    5.0  105.80                        NONE  Regular
70    12     12  165.98    2.0  128.06                        NONE  Regular
71    12     11   90.66    6.0  136.04                        NONE  Regular
72    13     15  108.24   13.0   81.44                        NONE  Regular
73    13     16  118.22   17.0  127.48                        NONE  Regular
74    13      5  109.90    7.0   82.22                        NONE  Regular
75    13      2  130.94    8.0  149.90                        NONE  Regular
76    13      6  105.86   12.0   91.50                        NONE  Regular
77    13      1  139.22   11.0   79.86                        NONE  Regular
78    14      7  148.16    NaN     NaN             WINNERS_BRACKET  Playoff
79    14     16  125.16    1.0   76.00             WINNERS_BRACKET  Playoff
80    14      5  153.42    6.0  120.96             WINNERS_BRACKET  Playoff
81    14     15  144.68    NaN     NaN             WINNERS_BRACKET  Playoff
82    14      2  149.00    8.0   90.10   LOSERS_CONSOLATION_LADDER  Playoff
83    14     12   85.62   17.0  129.72   LOSERS_CONSOLATION_LADDER  Playoff
84    14     11   91.34   13.0   67.16   LOSERS_CONSOLATION_LADDER  Playoff
85    15      7  182.56   16.0  165.62             WINNERS_BRACKET  Playoff
86    15     15  148.62    5.0  136.62             WINNERS_BRACKET  Playoff
87    15      1   88.42    6.0   99.48  WINNERS_CONSOLATION_LADDER  Playoff
88    15      2  136.68   17.0  121.90   LOSERS_CONSOLATION_LADDER  Playoff
89    15      8  145.04   11.0  112.60   LOSERS_CONSOLATION_LADDER  Playoff
90    15     12  125.40   13.0  123.02   LOSERS_CONSOLATION_LADDER  Playoff
91    16      7  123.42   15.0   80.16             WINNERS_BRACKET  Playoff
92    16      5  109.40   16.0  123.40  WINNERS_CONSOLATION_LADDER  Playoff
93    16      1  128.94    6.0  128.96  WINNERS_CONSOLATION_LADDER  Playoff
94    16      2  102.02    8.0   99.26   LOSERS_CONSOLATION_LADDER  Playoff
95    16     12   83.74   17.0  151.46   LOSERS_CONSOLATION_LADDER  Playoff
96    16     11   84.46   13.0   80.04   LOSERS_CONSOLATION_LADDER  Playoff

[97 rows x 7 columns]

相关问题 更多 >