简体   繁体   中英

Python - pd.DataFrame returning NaN values

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!

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()})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM