Добавление значений словаря в фрейм данных через цикл For

#python #dataframe #dictionary #for-loop #append

#python #фрейм данных #словарь #for-цикл #добавить

Вопрос:

Я ищу доступ к значениям словаря и добавляю их к существующему фрейму данных. Мой словарь выглядит следующим образом: данные -> (10 разных игр) -> каждая игра имеет (commence_time, домашняя команда, сайты, sites_count, sport_key, sport_nice, команды) -> каждый сайт (всего ~ 17 сайтов) имеет -> (last_update, odds, site_key, site_nice), тогда шансы имеют -> h2h, который содержит два числа

Я хочу:

  • цикл по словарю «данные»
  • найдите значения коэффициентов 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. Можем ли мы получить образец данных odds_json?

2. Добавлен пример — спасибо!

Ответ №1:

Вы были почти правы в своем коде. Вы должны были сделать out = out.append({...}, index=False) для dict, который вы хотели добавить. В вашем коде была какая-то логическая ошибка. Я исправил это ниже (вместе с некоторыми исправлениями для удобства чтения):

 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