Home > database >  Error with shape of passed value to pandas dataframe
Error with shape of passed value to pandas dataframe

Time:10-03

I parsed a json object obtained from hidden api on website; after some trouble i get finally what i wanted but got an error creatinf the dataframe. Here's the code:

import pandas as pd
import requests
url = "https://ero.betfair.com/www/sports/exchange/readonly/v1/bymarket?_ak=nzIFcwyWhrlwYMrh&alt=json&currencyCode=USD&locale=en_GB&marketIds=1.203214047,1.203214188,1.203257683,1.203632485,1.203355173,1.203417246,1.203329951,1.203642227,1.203641848,1.203632042,1.203642486,1.203641589,1.203643135&rollupLimit=25&rollupModel=STAKE&types=MARKET_STATE,RUNNER_STATE,RUNNER_EXCHANGE_PRICES_BEST"
r = requests.get(url).json()


eventid = []
marketid = []
odds1 = []
odds2 = []
oddsx = []

for matches in r['eventTypes'][0]['eventNodes']:

    eventid.append(matches['eventId'])
    marketid.append(matches['marketNodes'][0]['marketId'])
    odds1.append(matches['marketNodes'][0]['runners'][0]['exchange']['availableToBack'][0]['price'])
    odds2.append(matches['marketNodes'][0]['runners'][1]['exchange']['availableToBack'][0]['price'])
    oddsx.append(matches['marketNodes'][0]['runners'][2]['exchange']['availableToBack'][0]['price'])

df = pd.DataFrame(eventid, marketid, odds1, odds2, oddsx)

`The error i got is: "Shape of passed values is (13, 1), indices imply (13, 13)" In fact if i run for example odds1 i got this:

Out[261]: [1.45, 2.46, 1.82, 1.84, 1.41, 1.16, 1.84, 3.25, 4.1, 2.18, 2.96, 2.72, 3.0]

Expected is  [1.45
              2.46
              1.82
              ...]

like other eventid and marketid data, don't know why because i did the append the same way...any idea why and ho to get data as expected? Thanks!

json viewer

This should be the first part of the dicitonary: {"currencyCode":"USD","eventTypes":[{"eventTypeId":1,"eventNodes":[{"eventId":31733655,"marketNodes":[{"marketId":"1.203214047","isMarketDataDelayed":true,"state":{"betDelay":5,"bspReconciled":false,"complete":true,"inplay":true,"numberOfWinners":1,"numberOfRunners":3,"numberOfActiveRunners":0,"totalMatched":0.0,"totalAvailable":0.0,"crossMatching":false,"runnersVoidable":false,"version":4812185735,"status":"CLOSED"},"runners":[{"selectionId":47999,"handicap":0.0,"state":{"sortPriority":1,"status":"WINNER"},"exchange":{}},{"selectionId":48351,"handicap":0.0,"state":{"sortPriority":2,"status":"LOSER"},"exchange":{}},{"selectionId":58805,"handicap":0.0,"state":{"sortPriority":3,"status":"LOSER"},"exchange":{}}],"isMarketDataVirtual":false}]},{"eventId":31733656,"marketNodes":[{"marketId":"1.203214188","isMarketDataDelayed":true,"state":{"betDelay":5,"bspReconciled":false,"complete":true,"inplay":true,"numberOfWinners":1,"numberOfRunners":3,"numberOfActiveRunners":0,"totalMatched":0.0,"totalAvailable":0.0,"crossMatching":false,"runnersVoidable":false,"version":4813089879,"status":"CLOSED"},"runners":[{"selectionId":48317,"handicap":0.0,"state":{"sortPriority":1,"status":"LOSER"},"exchange":{}},{"selectionId":63908,"handicap":0.0,"state":{"sortPriority":2,"status":"LOSER"},"exchange":{}},{"selectionId":58805,"handicap":0.0,"state":{"sortPriority":3,"status":"WINNER"},"exchange":{}}],"isMarketDataVirtual":false}]},{"eventId":31735848,"marketNodes":[{"marketId":"1.203257683","isMarketDataDelayed":true,"state":{"betDelay":0,"bspReconciled":false,"complete":true,"inplay":false,"numberOfWinners":1,"numberOfRunners":3,"numberOfActiveRunners":3,"lastMatchTime":"2022-10-02T19:38:11.620Z","totalMatched":49380.28488825905,"totalAvailable":336698.8504071475,"crossMatching":true,"runnersVoidable":false,"version":4813316847,"status":"OPEN"},"runners":[{"selectionId":48461,"handicap":0.0,"state":{"sortPriority":1,"lastPriceTraded":1.83,"totalMatched":0.0,"status":"ACTIVE"},"exchange":{"availableToBack":[{"price":1.81,"size":385.8},{"price":1.8,"size":4509.05},{"price":1.79,"size":358.73}],"availableToLay":[{"price":1.83,"size":944.49},{"price":1.84,"size":1130.96},{"price":1.85,"size":340.52}]}},{"selectionId":39674645,"handicap":0.0,"state":{"sortPriority":2,"lastPriceTraded":4.7,"totalMatched":0.0,"status":"ACTIVE"},"exchange":{"availableToBack":[{"price":4.6,"size":421.05},{"price":4.5,"size":1157.23},{"price":4.4,"size":523.69}],"availableToLay":[{"price":4.7,"size":939.14},{"price":4.8,"size":1347.8},{"price":4.9,"size":2327.07}]}},{"selectionId":58805,"handicap":0.0,"state":{"sortPriority":3,"lastPriceTraded":4.3,"totalMatched":0.0,"status":"ACTIVE"},"exchange":{"availableToBack":[{"price":4.2,"size":1208.24},{"price":4.1,"size":3977.22},{"price":4.0,"size":3579.21}],"availableToLay":[{"price":4.3,"size":4990.36},{"price":4.4,"size":5192.82},{"price":4.5,"size":6148.02}]}}],"isMarketDataVirtual":true}]}

CodePudding user response:

You could try initializing the dataframe in the following way:

df = pd.DataFrame({
    'eventid': eventid,
    'market_id': marketid,
    'odds1': odds1,
    'odds2': odds2,
    'oddsx': oddsx
})
  • Related