I have this code:
import urllib.request, json
url = "https://api.wto.org/timeseries/v1/indicator_categories?lang=1"
hdr ={
# Request headers
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '21cda66d75fc4010b8b4d889f4af6ccd',
}
req = urllib.request.Request(url, headers=hdr)
req.get_method = lambda: 'GET'
response = urllib.request.urlopen(req)
#print(response.getcode())
var = print(response.read().decode('ASCII'))
I tried this:
import pandas as pd
df = pd.DataFrame(var)
I am only getting an empty dataframe. The data is lost somehow. What am I doing wrong?
You are missing the code to parse the JSON data from the URL into a Python dictionary before converting it to a Pandas DataFrame. Here is the modified code:
import urllib.request
import json
import pandas as pd
url = "https://api.wto.org/timeseries/v1/indicator_categories?lang=1"
hdr = {
'Cache-Control': 'no-cache',
'Ocp-Apim-Subscription-Key': '21cda66d75fc4010b8b4d889f4af6ccd'
}
req = urllib.request.Request(url, headers=hdr)
req.get_method = lambda: 'GET'
response = urllib.request.urlopen(req)
data = json.loads(response.read())
df = pd.DataFrame(data)
print(df)
This code snippet will correctly parse the JSON data from the API URL and convert it into a Pandas DataFrame.