I'm importing data from Alpha_Vantage in JSON format:
{
"Meta Data": {
"1. Information": "Daily Prices (open, high, low, close) and Volumes",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2018-08-02",
"4. Output Size": "Compact",
"5. Time Zone": "US/Eastern"
},
"Time Series (Daily)": {
"2018-08-02": {
"1. open": "105.4000",
"2. high": "108.0900",
"3. low": "104.8400",
"4. close": "107.5700",
"5. volume": "26080662"
},...
I want to extract different data from different tickers and combine them having the date as index and the "4. close" column of each ticker. So far this is what I have:
from alpha_vantage.timeseries import TimeSeries
from pprint import pprint
tickers = ['KHC', 'TSLA']
for t in range(len(tickers)):
ts = TimeSeries(key='my_api_key', output_format='pandas')
data, meta_data = ts.get_daily(symbol= tickers[t],
outputsize='compact')
accu = data['4. close'].head()
data_merged = data.merge(accu.to_frame(), how='left'\
, left_on='date'
, right_index=True)
pprint(data_merged.head)
At the moment there is a key error for 'date' in left_on, even though when printing a single ticker the key appears in the column. Tying another key just messes the data. Any idea? Also, how to print the ticker name at the top of every column?
accu.to_frame().head()show?datais your dataframe. In your code fragment, you attempt to merge it with its own head, which makes no sense. What you want is to merge the dataframes obtained at each loop iteration.