Как проанализировать данные коэффициентов ставок из API для создания pandas фрейма данных? - PullRequest
0 голосов
/ 08 марта 2020

Я новичок в кодировании и просто изучаю API и json. Я надеюсь создать pandas фрейм данных, который будет выглядеть следующим образом:

Name            Over        Line      Under

Al Horford      -125         13        -103

Andrew Wiggins  -130         20        +100

etc.

Я могу очистить все имена игроков:

import requests
import pandas as pd

url = 'https://betbuilder.digitalsportstech.com/api/feed'

headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}

payload = {
    'betType': 'in,18,19',
    'gameId': 'in,114109,114110,114111,114112,114113,114114',
    'isActive': '1',
    'limit': '9999',
    'sb': 'betonline',
    'tz': '-8'}

jsonData = requests.get(url, headers=headers, params=payload).json()

players_names = []

def get_names():
    data = jsonData['data']
    for d in data:
        name = d['player1']['name']
        print(name)
        players_names.append(d)

get_names()

Я не могу найти однако, данные ставок «за» и «за»

Я ищу 4 источника данных:

1) Имя: мне удалось найти имена игроков с выводом выше.

2) Сверх: я не могу найти эти данные.

3) Строка: мне удалось найти эти данные под заголовком «значение».

4) Под: я не могу найти эти данные.

В общем, я ищу помощь в поиске данных за и над. Затем я хотел бы объединить эти данные с соответствующими данными имени и данными строки. Любая помощь будет принята с благодарностью. Заранее спасибо за ваше время!

1 Ответ

1 голос
/ 09 марта 2020

Данные Under и Over выглядят так, как будто они есть. Он находится внутри клавиши odds и определяется betType. betType = 18 равно Under, а betType = 19 равно Over.

import pandas as pd
import json
from pandas.io.json import json_normalize
import requests
import numpy as np



headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36'}


# Get Game IDs
url = 'https://betbuilder.digitalsportstech.com/api/latestGames'
payload = {
        'leagueId': '123',
        'order': 'asc',
        'sb': 'betonline',
        'sort': 'date',
        'status': 'in,1,2,3',
        'tz': '0'}

jsonData = requests.get(url, headers=headers, params=payload).json()
gameIds = []
for game in jsonData['data']:
    gameIds.append(str(game['id']))
gameIds = ','.join(gameIds)




# Get Player Data
url = 'https://betbuilder.digitalsportstech.com/api/feed'
payload = {
    'betType': 'in,18,19',
    'gameId': 'in,%s' %gameIds,
    'isActive': '1',
    'limit': '9999',
    'sb': 'betonline',
    'tz': '-8'}

jsonData = requests.get(url, headers=headers, params=payload).json()
if len(jsonData['data']) == 0:
    raise ValueError("O/U Markets for this league will be available as soon as possible\nCheck back soon")


def flatten_df(data):
    finalOutput = pd.DataFrame()
    try:
        data = json_normalize(data)
        print ('Parsing the data...')
    except:
        data=data

    def unpack(temp_df, item):
        try:
            temp_df = json_normalize(item)
            return temp_df
        except:
            pass

    def iterrate(temp_df, row):
        for col, item in row.iteritems():
            if type(item) != list and type(item) != dict or col == 'statistic.phraseTitle':
                if len([ x for x in temp_df.columns if col.startswith(x) ]) > 0:
                        col = col + '_%s' %(len([ x for x in temp_df.columns if col.startswith(x) ]))
                try:
                    temp_df.loc[0,col] = item
                except:
                    temp_df.loc[0,col] = ', '.join(item)
                temp_df = temp_df.ffill()

            elif type(item) == list or type(item) == dict:
                temp_dfa = unpack(temp_df, item)
                temp_dfa.columns = col + '_' + temp_dfa.columns
                idxList = list(temp_df.index.values)
                alpha = temp_dfa.copy()
                merge_df = pd.DataFrame()
                for idx in idxList:
                    alpha.index = [idx]*len(alpha)
                    merge_df = merge_df.append(alpha, sort=False)


                temp_df = pd.merge(temp_df, merge_df, how='outer', left_index=True, right_index=True)
                temp_df = temp_df.reset_index(drop=True)
        return temp_df

    for idx, row in data.iterrows():
        temp_df = pd.DataFrame()
        finalOutput = finalOutput.append(iterrate(temp_df, row),sort=False).reset_index(drop=True)

    continueUnpacking = False
    for idx, row in finalOutput.iterrows():
        if len([ x for x, y in row.iteritems() if type(y) == list or type(y) == dict ]) > 0:
            print ('Still untangling...')
            continueUnpacking = True
            break

    if continueUnpacking == True:
        finalOutput = flatten_df(finalOutput)  


    return finalOutput

results = flatten_df(jsonData['data']) 




playersData = {}
for idx, row in results.iterrows():
    if row['player1.name'] not in list(playersData.keys()):
        playersData[row['player1.name']] = {}

    if row['statistic.title'] not in list(playersData[row['player1.name']].keys()):
        playersData[row['player1.name']][row['statistic.title']] = {'Line': row['markets_value']}

    # Under
    if row['betType'] == 18:
        playersData[row['player1.name']][row['statistic.title']].update({'Under': row['markets_odds']})

    # Over
    if row['betType'] == 19:
        playersData[row['player1.name']][row['statistic.title']].update({'Over': row['markets_odds']})

df = pd.DataFrame()
for player, v in playersData.items():
    temp_df = pd.DataFrame(v).T.reset_index().rename(columns={'index':'statistic.title'})
    temp_df['Name'] = player
    temp_df = temp_df[['Name', 'statistic.title', 'Under', 'Line', 'Over']]
    df = df.append(temp_df, sort=False).reset_index(drop=True)

Вывод:

Вот сайт и соответствующие выходные данные:

enter image description here

print (df)
               Name               statistic.title  Under  Line  Over
0   Devonte' Graham               Pts + Reb + Ast   1.90  28.0  1.86
1   Devonte' Graham  Three Point Field Goals Made   1.67   2.5  2.15
2   Devonte' Graham                        Points   1.86  17.5  1.90
3   Devonte' Graham                       Assists   1.65   6.5  2.18
4     Miles Bridges               Pts + Reb + Ast   1.88  24.0  1.88
5     Miles Bridges  Three Point Field Goals Made   1.85   1.5  1.91
6     Miles Bridges                Total Rebounds   1.88   6.0  1.88
7     Miles Bridges                        Points   1.81  15.5  1.96
8        Trae Young                       Assists   1.70   9.5  2.10
9        Trae Young  Three Point Field Goals Made   2.15   3.5  1.67
10       Trae Young               Pts + Reb + Ast   1.87  41.5  1.89
11       Trae Young                        Points   1.89  27.5  1.87
12    Kevin Huerter                        Points   1.92  13.5  1.84
13    Kevin Huerter  Three Point Field Goals Made   2.04   2.5  1.74
14    Kevin Huerter               Pts + Reb + Ast   1.85  21.5  1.91
15     Terry Rozier                        Points   1.95  19.5  1.81
16     Terry Rozier               Pts + Reb + Ast   1.88  27.5  1.88
17     Terry Rozier                Total Rebounds   2.24   4.5  1.62
18     Terry Rozier  Three Point Field Goals Made   1.95   2.5  1.81
19      Cody Zeller                        Points   1.77  10.5  2.00
20      Cody Zeller                Total Rebounds   1.74   6.5  2.05
21      Cody Zeller               Pts + Reb + Ast   1.82  19.5  1.94
22     John Collins                Total Rebounds   1.77   9.5  2.00
23     John Collins  Three Point Field Goals Made   2.12   1.5  1.69
24     John Collins               Pts + Reb + Ast   1.85  34.5  1.91
25     John Collins                        Points   1.86  22.5  1.90
26  P.J. Washington  Three Point Field Goals Made   1.88   1.5  1.88
27  P.J. Washington                Total Rebounds   1.91   5.5  1.85
28  P.J. Washington               Pts + Reb + Ast   1.80  21.5  1.97
29  P.J. Washington                        Points   1.84  13.5  1.92
...