Reputation: 3494
I am attempting to use an API request to gather weather data and ultimately save the data to a CSV file with pandas.to_csv. The API request is working, but I get this error when I am trying to convert the API response from a dictionary to pandas. Any tips for what I am doing wrong is greatly appreciated:
TypeError: unhashable type: 'dict'
The last line in the code is what is messed up and the shell is returning an error:
from urllib.request import urlopen
import json
import pandas as pd
api_key = ""
date = "20170601"
zip_code = "53711"
response = urlopen("http://api.wunderground.com/api/%s/history_%s/q/%s.json" % (api_key, date, zip_code))
json_data = response.read().decode('utf-8', 'replace')
data = json.loads(json_data)
for observation in data['history']['observations']:
print("Date/Time: " + observation['date']['pretty'])
print("Temperature: " + observation['tempi'])
print("Humidity: " + observation['hum'])
df = pd.DataFrame([data], columns=data.keys())
The screen shot is what the data looks like in the shell output:
Upvotes: 2
Views: 1878
Reputation: 862511
You can use json_normalize
with filtering columns, also is possible convert date column to_datetime
:
from pandas.io.json import json_normalize
df = json_normalize(data['history']['observations'])
df = df[['date.pretty','tempi','hum']]
df['date.pretty'] = pd.to_datetime(df['date.pretty'])
print (df.head())
date.pretty tempi hum
0 2017-06-01 00:15:00 49.8 83
1 2017-06-01 00:35:00 50.0 82
2 2017-06-01 00:55:00 49.1 85
3 2017-06-01 01:15:00 49.3 83
4 2017-06-01 01:35:00 48.2 85
Upvotes: 1