2017-11-05 5 views
2

저는 여전히 파이썬 신참이며 JSON 데이터 구조로 작업 할 때는 당황합니다. 예를 들어 Alpha Vantage에서 얻은 데이터를 추가 처리를 위해 DataFrame에로드하려고했습니다. json으로는 다음과 같습니다 : 나는 날짜를 포함하고 닫기 만 조정 된 dataframe 구축을 위해 노력하고JSON 데이터에서 pandas DataFrame을 빌드하십시오.

{ 
"Meta Data": { 
    "1. Information": "Daily Time Series with Splits and Dividend Events", 
    "2. Symbol": "SHAK", 
    "3. Last Refreshed": "2017-11-03", 
    "4. Output Size": "Compact", 
    "5. Time Zone": "US/Eastern" 
}, 
"Time Series (Daily)": { 
    "2017-11-03": { 
     "1. open": "35.9000", 
     "2. high": "37.0700", 
     "3. low": "35.5600", 
     "4. close": "36.9800", 
     "5. adjusted close": "36.9800", 
     "6. volume": "874351", 
     "7. dividend amount": "0.0000", 
     "8. split coefficient": "1.0000" 
    }, 
    "2017-11-02": { 
     "1. open": "38.5000", 
     "2. high": "38.7000", 
     "3. low": "35.4300", 
     "4. close": "35.9000", 
     "5. adjusted close": "35.9000", 
     "6. volume": "1860695", 
     "7. dividend amount": "0.0000", 
     "8. split coefficient": "1.0000" 
    }, 
    "2017-11-01": { 
     "1. open": "37.8800", 
     "2. high": "38.2600", 
     "3. low": "36.9600", 
     "4. close": "37.1500", 
     "5. adjusted close": "37.1500", 
     "6. volume": "1350008", 
     "7. dividend amount": "0.0000", 
     "8. split coefficient": "1.0000" 
    },... 

.

from urllib.request import Request, urlopen 
import json 
import pandas as pd 
from pandas.io.json import json_normalize 

request=Request('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=SHAK&apikey=topsecret') 
response=urlopen(request) 

x=response.read() 
data=json.loads(x) 
df=pd.read_json(x,typ='series') 

이 그래서 여기에 메타 데이터가 이미 시계열에서 분리

Meta Data    {'1. Information': 'Daily Time Series with Spl... 
Time Series (Daily) {'2017-11-03': {'1. open': '96.1700', '2. high... 
dtype: object 

뭔가를 반환합니다. 하지만 어떻게하면 시계열을 통해 매일 "조정 된 닫기"에 액세스 할 수 있습니까?

누군가가 나를 도와 줄 수 있다면 정말 좋을 것입니다!

+0

아, 내가이 토론을 발견 https://stackoverflow.com/questions/44742003/alphavantage -api-stock market-indices 그리고 그것은 도움이 될 것입니다 ... – RazzleDazzle

답변

0

이미 json 모듈을 사용하여 JSON을 파싱 했으므로 다음과 같은 방식으로 DataFrame을 만든 다음 슬라이스하여 조정 된 닫기를 얻을 수 있습니다. 제공된 샘플 데이터로

request=Request('https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=SHAK&apikey=topsecret') 
response=urlopen(request) 

data=json.loads(response.read()) 
df=pd.DataFrame.from_dict(data['Time Series (Daily)'], orient="index") 

# Probably want that index to be a DatetimeIndex 
df.index = pd.to_datetime(df.index) 

# To get a pandas series that just has adjusted close, select that column 
adj_close = df['5. adjusted close'] 

, adj_close이처럼 보이는 팬더 시리즈가 될 것입니다 :

2017-11-01 37.1500 
2017-11-02 35.9000 
2017-11-03 36.9800 
Name: 5. adjusted close, dtype: object 
관련 문제