Merv Merzoug
Merv Merzoug

Reputation: 1237

Parse requests.get() output into a pandas dataframe

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!

Upvotes: 3

Views: 10481

Answers (1)

RandomHash
RandomHash

Reputation: 681

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())

Upvotes: 6

Related Questions