将 Alphavantage API 响应转换为 DataFrame
Convert Alphavantage API Response to DataFrame
我在 SO 上查找了其他相关主题,只找到了类似的问题,但没有任何帮助。
我正在查询 AlphaVantage 以获取股票数据。我收到了数据并进行了解码,但由于格式问题目前无法转换为 pandas 数据帧。响应采用以下形式:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "AAPL",
"3. Last Refreshed": "2021-04-26",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2021-04-26": {
"1. open": "134.8300",
"2. high": "135.0600",
"3. low": "133.5600",
"4. close": "134.7200",
"5. volume": "66905069"
},
"2021-04-23": {
"1. open": "132.1600",
"2. high": "135.1200",
"3. low": "132.1600",
"4. close": "134.3200",
"5. volume": "78756779"
},
"2021-04-22": {
"1. open": "133.0400",
"2. high": "134.1500",
"3. low": "131.4100",
"4. close": "131.9400",
"5. volume": "84566456"
},
在 运行 下面的代码之后:
import requests as rq
import json
base_url = "https://www.alphavantage.co/query?"
params = {"function":function, "symbol":symbol, "outputsize":output_size, "datatype":data_type, "apikey":api_key}
response = rq.get(base_url, params=params)
data_str = data_bytes.decode("utf-8")
我在尝试将数据加载到数据帧时出现问题:
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict.items())
df.head()
Returns:
0 1
0 Meta Data {'1. Information': 'Daily Prices (open, high, ...
1 Time Series (Daily) {'2021-04-26': {'1. open': '134.8300', '2. hig...
还有...
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict)
df.head()
Returns:
Meta Data Time Series (Daily)
1. Information Daily Prices (open, high, low, close) and Volumes NaN
2. Symbol AAPL NaN
3. Last Refreshed 2021-04-26 NaN
4. Output Size Full size NaN
5. Time Zone US/Eastern NaN
哪个都不可用。我正在寻找以下形式的数据框:
date open high low close volume
有没有办法将响应转换成这种格式?
因为您只需要 Time Series (Daily)
个值。所以在创建dataframe的时候可以直接使用
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict["Time Series (Daily)"])
df = df.T # Transpose Dataframe for desired results
我在 SO 上查找了其他相关主题,只找到了类似的问题,但没有任何帮助。
我正在查询 AlphaVantage 以获取股票数据。我收到了数据并进行了解码,但由于格式问题目前无法转换为 pandas 数据帧。响应采用以下形式:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "AAPL",
"3. Last Refreshed": "2021-04-26",
"4. Output Size": "Full size",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2021-04-26": {
"1. open": "134.8300",
"2. high": "135.0600",
"3. low": "133.5600",
"4. close": "134.7200",
"5. volume": "66905069"
},
"2021-04-23": {
"1. open": "132.1600",
"2. high": "135.1200",
"3. low": "132.1600",
"4. close": "134.3200",
"5. volume": "78756779"
},
"2021-04-22": {
"1. open": "133.0400",
"2. high": "134.1500",
"3. low": "131.4100",
"4. close": "131.9400",
"5. volume": "84566456"
},
在 运行 下面的代码之后:
import requests as rq
import json
base_url = "https://www.alphavantage.co/query?"
params = {"function":function, "symbol":symbol, "outputsize":output_size, "datatype":data_type, "apikey":api_key}
response = rq.get(base_url, params=params)
data_str = data_bytes.decode("utf-8")
我在尝试将数据加载到数据帧时出现问题:
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict.items())
df.head()
Returns:
0 1
0 Meta Data {'1. Information': 'Daily Prices (open, high, ...
1 Time Series (Daily) {'2021-04-26': {'1. open': '134.8300', '2. hig...
还有...
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict)
df.head()
Returns:
Meta Data Time Series (Daily)
1. Information Daily Prices (open, high, low, close) and Volumes NaN
2. Symbol AAPL NaN
3. Last Refreshed 2021-04-26 NaN
4. Output Size Full size NaN
5. Time Zone US/Eastern NaN
哪个都不可用。我正在寻找以下形式的数据框:
date open high low close volume
有没有办法将响应转换成这种格式?
因为您只需要 Time Series (Daily)
个值。所以在创建dataframe的时候可以直接使用
data_dict = json.loads(data_str)
df = pd.DataFrame(data_dict["Time Series (Daily)"])
df = df.T # Transpose Dataframe for desired results