通过 For 循环将字典值附加到 DataFrame

时间:2021-02-16 23:37:29

标签: python dataframe dictionary for-loop append

我希望访问字典值并将它们附加到现有数据帧。我的字典是这样的: 数据 ->(10 种不同的游戏)-> 每场比赛都有(开始时间,家庭团队,站点,站点计数,运动关键,运动很好,团队) -> 每个站点(总共约 17 个站点)有 -> (last_update,odds,site_key,site_nice) 然后赔率有 -> h2h 包含两个数字

我希望:

  • 遍历字典“data”
  • 查找特定“站点”的 h2h 赔率值
  • 将这些附加到带有团队名称的现有数据集

请指教。谢谢!


for i in range(len(odds_json['data'])):
    for j in range(len(odds_json['data'][i]['sites'])):
        if odds_json['data'][i]['sites'][j]['site_key'] is 'fanduel':
            odds = odds.append({'home_team': odds_json['data'][i]['teams'][1], 'visitor_team': odds_json['data'][i]['teams'][0]}, ignore_index=True) #, 'home_odds': odds_json['data'][i]['sites'][j]['odds']['h2h'][1], 'visitor_odds': odds_json['data'][i]['sites'][j]['odds']['h2h'][0]}, ignore_index=True)
            True and odds.append({'home_team': odds_json['data'][i]['teams'][1], 'visitor_team': odds_json['data'][i]['teams'][0], 'home_odds': odds_json['data'][i]['sites'][j]['odds']['h2h'][1], 'visitor_odds': odds_json['data'][i]['sites'][j]['odds']['h2h'][0]}, ignore_index=True)

odds_json['data'] 示例:

{'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Colorado Avalanche', 'Vegas Golden Knights'],
  'commence_time': 1613531700,
  'home_team': 'Vegas Golden Knights',
  'sites': [{'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [104, -115]}},
   {'site_key': 'mybookieag',
    'site_nice': 'MyBookie.ag',
    'last_update': 1613516369,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'fanduel',
    'site_nice': 'FanDuel',
    'last_update': 1613516409,
    'odds': {'h2h': [102, -120]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [104, -115]}},
   {'site_key': 'pointsbetus',
    'site_nice': 'PointsBet (US)',
    'last_update': 1613516418,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [100, -117]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [-107, -113]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [100, -117]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [100, -117]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [163, 133, 310]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [-105, -110]}},
   {'site_key': 'betfair',
    'site_nice': 'Betfair',
    'last_update': 1613516341,
    'odds': {'h2h': [104, -114], 'h2h_lay': [114, -103]}},
   {'site_key': 'intertops',
    'site_nice': 'Intertops',
    'last_update': 1613516352,
    'odds': {'h2h': [100, -120]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [-105, -120]}}],
  'sites_count': 17},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Los Angeles Kings', 'Minnesota Wild'],
  'commence_time': 1613531700,
  'home_team': 'Los Angeles Kings',
  'sites': [{'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [130, -150]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [130, -150]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [137, -152]}},
   {'site_key': 'mybookieag',
    'site_nice': 'MyBookie.ag',
    'last_update': 1613516369,
    'odds': {'h2h': [125, -145]}},
   {'site_key': 'fanduel',
    'site_nice': 'FanDuel',
    'last_update': 1613516409,
    'odds': {'h2h': [128, -152]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [130, -152]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [130, -150]}},
   {'site_key': 'pointsbetus',
    'site_nice': 'PointsBet (US)',
    'last_update': 1613516418,
    'odds': {'h2h': [130, -150]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [137, -151]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [125, -145]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [130, -152]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [128, -148]}},
   {'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [205, 107, 310]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [125, -145]}},
   {'site_key': 'betfair',
    'site_nice': 'Betfair',
    'last_update': 1613516341,
    'odds': {'h2h': [124, -137], 'h2h_lay': [138, -123]}},
   {'site_key': 'intertops',
    'site_nice': 'Intertops',
    'last_update': 1613516352,
    'odds': {'h2h': [130, -150]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [120, -154]}}],
  'sites_count': 17},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Carolina Hurricanes', 'Florida Panthers'],
  'commence_time': 1613599200,
  'home_team': 'Carolina Hurricanes',
  'sites': [{'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [105, 210, 320]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [-150, 129]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [-150, 129]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [-150, 129]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [-167, 130]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [-149, 134]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [-148, 134]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [-155, 135]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [-160, 140]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [-160, 140]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [-155, 135]}},
   {'site_key': 'caesars',
    'site_nice': 'Caesars',
    'last_update': 1613516377,
    'odds': {'h2h': [-159, 135]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [-160, 135]}},
   {'site_key': 'fanduel',
    'site_nice': 'FanDuel',
    'last_update': 1613516409,
    'odds': {'h2h': [-164, 138]}}],
  'sites_count': 14},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Ottawa Senators', 'Toronto Maple Leafs'],
  'commence_time': 1613606400,
  'home_team': 'Toronto Maple Leafs',
  'sites': [{'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [380, -192, 440]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [240, -286]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [240, -286]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [240, -286]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [240, -333]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [260, -303]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [260, -295]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [250, -300]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [250, -300]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [255, -310]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [252, -310]}},
   {'site_key': 'caesars',
    'site_nice': 'Caesars',
    'last_update': 1613516377,
    'odds': {'h2h': [255, -312]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [275, -350]}}],
  'sites_count': 13},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Chicago Blackhawks', 'Detroit Red Wings'],
  'commence_time': 1613608200,
  'home_team': 'Detroit Red Wings',
  'sites': [{'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [125, 175, 310]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [-127, 110]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [-127, 110]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [-127, 110]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [-139, 110]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [-127, 114]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [-126, 114]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [-130, 110]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [-130, 110]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [-130, 110]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [-130, 110]}},
   {'site_key': 'caesars',
    'site_nice': 'Caesars',
    'last_update': 1613516377,
    'odds': {'h2h': [-130, 110]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [-130, 110]}}],
  'sites_count': 13},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Edmonton Oilers', 'Winnipeg Jets'],
  'commence_time': 1613617200,
  'home_team': 'Edmonton Oilers',
  'sites': [{'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [116, 180, 325]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [-136, 118]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [-136, 118]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [-136, 118]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [-143, 110]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [-141, 125]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [-139, 126]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [-140, 120]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [-140, 120]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [-140, 120]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [-140, 120]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [-140, 120]}}],
  'sites_count': 12},
 {'sport_key': 'icehockey_nhl',
  'sport_nice': 'NHL',
  'teams': ['Calgary Flames', 'Vancouver Canucks'],
  'commence_time': 1613617200,
  'home_team': 'Calgary Flames',
  'sites': [{'site_key': 'unibet',
    'site_nice': 'Unibet',
    'last_update': 1613516407,
    'odds': {'h2h': [105, 210, 310]}},
   {'site_key': 'betrivers',
    'site_nice': 'BetRivers',
    'last_update': 1613516374,
    'odds': {'h2h': [-155, 133]}},
   {'site_key': 'draftkings',
    'site_nice': 'DraftKings',
    'last_update': 1613516351,
    'odds': {'h2h': [-155, 133]}},
   {'site_key': 'sugarhouse',
    'site_nice': 'SugarHouse',
    'last_update': 1613516362,
    'odds': {'h2h': [-155, 133]}},
   {'site_key': 'foxbet',
    'site_nice': 'FOX Bet',
    'last_update': 1613516352,
    'odds': {'h2h': [-161, 125]}},
   {'site_key': 'betonlineag',
    'site_nice': 'BetOnline.ag',
    'last_update': 1613516427,
    'odds': {'h2h': [-141, 127]}},
   {'site_key': 'lowvig',
    'site_nice': 'LowVig.ag',
    'last_update': 1613516346,
    'odds': {'h2h': [-140, 127]}},
   {'site_key': 'bovada',
    'site_nice': 'Bovada',
    'last_update': 1613516422,
    'odds': {'h2h': [-145, 125]}},
   {'site_key': 'williamhill_us',
    'site_nice': 'William Hill (US)',
    'last_update': 1613516431,
    'odds': {'h2h': [-150, 130]}},
   {'site_key': 'gtbets',
    'site_nice': 'GTbets',
    'last_update': 1613516405,
    'odds': {'h2h': [-150, 130]}},
   {'site_key': 'bookmaker',
    'site_nice': 'Bookmaker',
    'last_update': 1613516375,
    'odds': {'h2h': [-155, 135]}},
   {'site_key': 'betmgm',
    'site_nice': 'BetMGM',
    'last_update': 1613516386,
    'odds': {'h2h': [-155, 130]}}],
  'sites_count': 12}]

1 个答案:

答案 0 :(得分:0)

你的代码几乎是对的。您必须为要附加的 dict 执行 out = out.append({...}, index=False)。您的代码中存在一些逻辑错误。我在下面修复了它(以及一些固定的可读性):

for item in data:
    for site in item["sites"]:
        if site["site_key"] == "fanduel":
            df = df.append(
                {
                    "home_team": item["teams"][1],
                    "visitor_team": item["teams"][0],
                    "home_odds": site["odds"]["h2h"][1],
                    "visitor_odds": site["odds"]["h2h"][0],
                },
                ignore_index=True,
            )
print(df)
# Output
#    home_odds             home_team  visitor_odds         visitor_team
# 0     -120.0  Vegas Golden Knights         102.0   Colorado Avalanche
# 1     -152.0        Minnesota Wild         128.0    Los Angeles Kings
# 2      138.0      Florida Panthers        -164.0  Carolina Hurricanes