我尝试从Morningstar中获取数据并合并不同的股票,但是我不知道如何正确地合并数据。我想按日期组织它,但它只是将数据堆叠在一起。
print('test')
print('testing')
#this program will read data from morningstar and interpret them using pandas
import pandas as pd
import datetime
import numpy as np
import matplotlib.pylab as plt
pd.core.common.is_list_like = pd.api.types.is_list_like
import pandas_datareader.data as web
start = datetime.datetime(2010,1,1) #datetime is (year, month, day)
end = datetime.date.today()
#Getting data from morningstar
microsoft = pd.DataFrame(web.DataReader("MSFT", "morningstar", start, end))
apple = pd.DataFrame(web.DataReader("AAPL","morningstar", start, end))
google = pd.DataFrame(web.DataReader("GOOG", "morningstar", start, end))
stocks = pd.DataFrame({"MSFT": microsoft["Volume"],
"AAPL": apple["Volume"],
"GOOG": google["Volume"]})
print(stocks)
基本上我希望数据看起来像这样:
stock1 stock2 stock3
date1 123 345 234
date2 657 294 553
date3 786 321 933
但是事实却是这样的:
stock1 stock2 stock3
date1 123 NaN NaN
date2 657 NaN NaN
date3 786 NaN NaN
date1 NaN 345 NaN
date2 NaN 294 NaN
date3 NaN 321 NaN
date1 NaN NaN 234
date2 NaN NaN 553
date3 NaN NaN 933
答案 0 :(得分:0)
创建新的数据框时,您可以在末尾添加reset_index
stocks = pd.DataFrame({"MSFT": microsoft["Volume"].reset_index(level=0,drop=True),
"AAPL": apple["Volume"].reset_index(level=0,drop=True),
"GOOG": google["Volume"].reset_index(level=0,drop=True)})