Python Forum
'int' object is not subscriptable after API call
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
'int' object is not subscriptable after API call
#1
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.
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.
Reply
#2
Hello,

Hope the update is good!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Bug TypeError: 'NoneType' object is not subscriptable TheLummen 4 679 Nov-27-2023, 11:34 AM
Last Post: TheLummen
  Help with python 'not subscriptable' error Extra 3 1,978 Dec-16-2022, 05:55 PM
Last Post: woooee
  TypeError: 'NoneType' object is not subscriptable syafiq14 3 5,168 Sep-19-2022, 02:43 PM
Last Post: Larz60+
  Message='int' object is not subscriptable DanielCook 4 2,263 Aug-10-2021, 05:21 PM
Last Post: deanhystad
  Bool Object is not Subscriptable quest 1 4,086 May-02-2021, 11:12 AM
Last Post: Yoriz
  Float Object is not Subscriptable quest 2 2,854 Apr-20-2021, 09:28 AM
Last Post: quest
  how to call an object in another function in Maya bstout 0 2,042 Apr-05-2021, 07:12 PM
Last Post: bstout
  TypeError: 'NoneType' object is not subscriptable Jmekubo 6 27,226 Sep-08-2020, 10:03 AM
Last Post: DigiTMG
  TypeError: 'type' object is not subscriptable Stef 1 4,442 Aug-28-2020, 03:01 PM
Last Post: Gribouillis
  'NoneType' object is not subscriptable Justchse 4 3,663 Aug-01-2020, 06:18 PM
Last Post: Justchse

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020