[英]Parse requests.get() output into a pandas dataframe
I am following a tutorial an am stuck at parsing the output of requests.get()我正在学习一个教程,但我一直在解析 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 以提取历史加密货币价格并将它们放入 pandas 数据框以进行进一步分析。
[API: https://www.cryptocompare.com/api/#-api-data-histoday-] [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!但我得到... PandasError: DataFrame 构造函数没有正确调用!
You can use the json package to convert to dict:您可以使用 json 包转换为 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:然而,正如 jonrsharpe 所指出的,一种更简单的方法是:
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.