TypeError: 'float' object is not subscriptable

crompnk
crompnk used Ask the Experts™
on
Hi,

Using Python I'm trying to read a json string and output it to a dataframe, but I get the following error:

TypeError: 'float' object is not subscriptable

code is here:
# We import the requests module which allows us to make the API call
import pandas as pd
import ast
import json
import requests

# Call API to pull data
url = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22'

response = requests.get(url = url)
response_data = response.json()

#data = response_data

data = """{  
   'coord':{  
      'lon':-0.13,
      'lat':51.51
   },
   'weather':[  
      {  
         'id':300,
         'main':'Drizzle',
         'description':'light intensity drizzle',
         'icon':'09d'
      }
   ],
   'base':'stations',
   'main':{  
      'temp':280.32,
      'pressure':1012,
      'humidity':81,
      'temp_min':279.15,
      'temp_max':281.15
   },
   'visibility':10000,
   'wind':{  
      'speed':4.1,
      'deg':80
   },
   'clouds':{  
      'all':90
   },
   'dt':1485789600,
   'sys':{  
      'type':1,
      'id':5091,
      'message':0.0103,
      'country':'GB',
      'sunrise':1485762037,
      'sunset':1485794875
   },
   'id':2643743,
   'name':'London',
   'cod':200
}"""

val = ast.literal_eval(data)
val1 = json.loads(json.dumps(val))
val2 = val1['main']['temp'][0]
dataset = pd.DataFrame(val2)
#OutputDataSet=dataset
#print(val1)
print(dataset)

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
This is because the val1['main']['temp'] at the line 60 is a temperature and not the array. The [0] after that should be removed if the val2 should store the temperature value. You can try to add print(repr(val1['main'])) before the line 60 to see what is processed.
crompnkData Management Specialist

Author

Commented:
This worked:

import pandas as pd
import json
import requests


# In[2]:


data = {  
   'coord':{  
      'lon':-0.13,
      'lat':51.51
   },
   'weather':[  
      {  
         'id':300,
         'main':'Drizzle',
         'description':'light intensity drizzle',
         'icon':'09d'
      }
   ],
   'base':'stations',
   'main':{  
      'temp':280.32,
      'pressure':1012,
      'humidity':81,
      'temp_min':279.15,
      'temp_max':281.15
   },
   'visibility':10000,
   'wind':{  
      'speed':4.1,
      'deg':80
   },
   'clouds':{  
      'all':90
   },
   'dt':1485789600,
   'sys':{  
      'type':1,
      'id':5091,
      'message':0.0103,
      'country':'GB',
      'sunrise':1485762037,
      'sunset':1485794875
   },
   'id':2643743,
   'name':'London',
   'cod':200
}


# In[6]:


val = json.loads(json.dumps(data))


# In[19]:


val1 = val['main']


# In[32]:


df = pd.DataFrame(list(val1.items()), index=[1, 2, 3, 4, 5])
df

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial