I am getting this error when I try to create a DataFrame from an API call. The API call itself is successful and returns data successfully..but not sure about the error after.
Error:
I have tried quite a few different things to create a DF.
import numpy as np import pandas as pd import requests import json from datetime import datetime import matplotlib.pyplot as plt import http.client
conn = http.client.HTTPSConnection("api.sportradar.us") conn.request("GET", "/nhl/trial/v7/en/seasons/2020/REG/teams/4416091c-0f24-11e2-8525-18a905767e44/analytics.json?api_key={apikey}") res = conn.getresponse() data = res.read() print(data.decode("utf-8"))placeholder for API key.
df = pd.json_normalize(data, 'id') df.head(10)
Error:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-49-51d43eff599e> in <module> ----> 1 df = pd.json_normalize(data, 'id') 2 df.head(10) ~\anaconda3\envs\Python_DS\lib\site-packages\pandas\io\json\_normalize.py in _json_normalize(data, record_path, meta, meta_prefix, record_prefix, errors, sep, max_level) 339 records.extend(recs) 340 --> 341 _recursive_extract(data, record_path, {}, level=0) 342 343 result = DataFrame(records) ~\anaconda3\envs\Python_DS\lib\site-packages\pandas\io\json\_normalize.py in _recursive_extract(data, path, seen_meta, level) 311 else: 312 for obj in data: --> 313 recs = _pull_records(obj, path[0]) 314 recs = [ 315 nested_to_record(r, sep=sep, max_level=max_level) ~\anaconda3\envs\Python_DS\lib\site-packages\pandas\io\json\_normalize.py in _pull_records(js, spec) 250 if has non iterable value. 251 """ --> 252 result = _pull_field(js, spec) 253 254 # GH 31507 GH 30145, if result is not Iterable, raise TypeError if not ~\anaconda3\envs\Python_DS\lib\site-packages\pandas\io\json\_normalize.py in _pull_field(js, spec) 241 result = result[field] 242 else: --> 243 result = result[spec] 244 return result 245 TypeError: 'int' object is not subscriptable
I have tried quite a few different things to create a DF.
Larz60+ write Sep-18-2021, 01:31 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Refrain from using images for code.
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Refrain from using images for code.