Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (17.6k points)

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: enter image description here

1 Answer

0 votes
by (41.4k points)

1.Use json_normalize with column filtering.

2.Then, convert date column to 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

If you wish to know about Python visit this Python Course.

Related questions

0 votes
1 answer
0 votes
2 answers
0 votes
2 answers
asked Sep 11, 2019 in Python by Sammy (47.6k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

30.5k questions

32.6k answers

500 comments

108k users

Browse Categories

...