3

I am following a tutorial an am stuck at parsing the output of requests.get()

My goal is to connect to the API below to pull historical crypto-currency prices and put them into a pandas dataframe for further analysis.

[API: https://www.cryptocompare.com/api/#-api-data-histoday-]

Here's what I have.

import requests
response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 



print(response.text)

Now I want to output into a dataframe...

pd.DataFrame.from_dict(response)

But I get... PandasError: DataFrame constructor not properly called!

2
  • response isn't a dictionary - maybe .from_dict(response.json())? Commented May 22, 2017 at 22:06
  • That gets me a bit closer! Now it is in a df, but the keys and values are all out of order... I need the timestamp to be in a datetime format I think then call the the index Commented May 22, 2017 at 22:09

1 Answer 1

6

You can use the json package to convert to dict:

import requests
from json import loads
import pandas as pd

response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 

dic = loads(response.text)

print(type(dic))

pd.DataFrame.from_dict(dic)

However as jonrsharpe noted, a much more simple way would be:

import requests
import pandas as pd

response = requests.get("https://min-api.cryptocompare.com/data/histodayfsym=ETC&tsym=USD&limit=10&aggregate=3&e=CCCAGG") 


print(type(response.json()))
pd.DataFrame.from_dict(response.json())
Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, i stand corrected :), i have update answer to reflect this

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.