0
votes

I'm trying to fetch prices of three stock tickers from morningstar and put them into a data frame, but pd.DataFrame keeps giving me NaN values.

all_data = {}

for ticker in ['AAL', 'ALK','WTI']:
     all_data[ticker] = data.DataReader(ticker, 'morningstar', '2014-06-01','2016-06-13')

enter image description here

price = pd.DataFrame({ticker: data['Close'] for ticker, data in all_data.items()})
print(price.head(5))

enter image description here

Ideally I want the pd.DataFrame to return a data frame of four columns (date, closing price for ticker 1, closing price for ticker 2 and closing price for ticker 3) but it kept returning NaN values for the second and third tickers.

I'm wondering how can I fix the code to get intended results?

Thank you very much!

1
You can make the code samples more readable by enclosing them in code format tags. See the tools above the edit bar or check out the help here. - Bill

1 Answers

1
votes

I think the dict value is dataframe, base on your image , so when you doing data['Close'] it will keep its index, different index concat will return NaN for miss match

all_data = {}

for ticker in ['AAL', 'ALK','WTI']:
     all_data[ticker] = data.DataReader(ticker, 'morningstar', '2014-06-01','2016-06-13')

price = pd.DataFrame({ticker: data['Close'].values for ticker, data in all_data.items()})