小编sky*_*ker的帖子

Python:从 JSON 响应的部分构建 DataFrame

我正在尝试开发一个应用程序来检索股票价格(以 JSON 格式),然后对其进行一些分析。我的问题是将 JSON 响应放入我可以工作的 pandas DataFrame 中。这是我的代码:

'''
References
http://stackoverflow.com/questions/6862770/python-3-let-json-object-  accept-bytes-or-let-urlopen-output-strings
'''
import json
import pandas as pd
from urllib.request import urlopen

#set API call
url = "https://www.quandl.com/api/v3/datasets/WIKI/AAPL.json?start_date=2017-01-01&end_date=2017-01-31"

#make call and receive response
response = urlopen(url).read().decode('utf8')
dataresponse = json.loads(response)

#check incoming
#print(dataresponse)

df = pd.read_json(dataresponse)

print(df)
Run Code Online (Sandbox Code Playgroud)

应用程序出现df = pd.read_json...错误TypeError: Expected String or Unicode

所以我认为这是第一个障碍。

第二是到达我需要去的地方。JSON 响应仅包含两个我感兴趣的数组,column_namesdata。如何仅提取这两个并将其放入 pandas DataFrame 中?

json dataframe python-3.x pandas

1
推荐指数
1
解决办法
2301
查看次数

标签 统计

dataframe ×1

json ×1

pandas ×1

python-3.x ×1