使用 pandas yahoo finance datareader 下载多只股票并将它们放入 DataFrame
Download multiple stocks with pandas yahoo finance datareader and putting them in a DataFrame
大家好,我想使用 Pandas 从 yahoo finance 下载多只股票。
但与此同时,我需要 仅保存每只股票的“Adj Close”列。
此外,我想创建一个包含所有这些“Adj Close”列的DataFrame,并将列名称设置为股票代码。
我尝试使用此代码,但卡住了。
import numpy as np
import pandas as pd
from datetime import datetime
import pandas_datareader.data as web
stocks = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
ls_key = 'Adj Close'
start = datetime(2014,1,1)
end = datetime(2015,1,1)
f = web.DataReader(stocks, 'yahoo',start,end)
f
希望有人能帮助我
df = f[[("Adj Close", s) for s in stocks]]
df.columns = df.columns.droplevel(level=0)
df
>>
Symbols ORCL TSLA IBM YELP MSFT
Date
2014-01-02 33.703285 30.020000 137.696884 67.919998 31.983477
2014-01-03 33.613930 29.912001 138.520721 67.660004 31.768301
2014-01-06 33.479893 29.400000 138.045746 71.720001 31.096956
2014-01-07 33.819431 29.872000 140.799240 72.660004 31.337952
2014-01-08 33.703274 30.256001 139.507858 78.419998 30.778502
... ... ... ... ...
2014-12-24 41.679443 44.452000 123.015839 53.000000 42.568497
2014-12-26 41.562233 45.563999 123.411110 52.939999 42.338593
2014-12-29 41.120468 45.141998 122.019974 53.009998 41.958347
2014-12-30 40.877041 44.445999 121.670265 54.240002 41.578117
2014-12-31 40.543465 44.481998 121.966751 54.730000 41.074078
大家好,我想使用 Pandas 从 yahoo finance 下载多只股票。
但与此同时,我需要 仅保存每只股票的“Adj Close”列。
此外,我想创建一个包含所有这些“Adj Close”列的DataFrame,并将列名称设置为股票代码。
我尝试使用此代码,但卡住了。
import numpy as np
import pandas as pd
from datetime import datetime
import pandas_datareader.data as web
stocks = ['ORCL', 'TSLA', 'IBM','YELP', 'MSFT']
ls_key = 'Adj Close'
start = datetime(2014,1,1)
end = datetime(2015,1,1)
f = web.DataReader(stocks, 'yahoo',start,end)
f
希望有人能帮助我
df = f[[("Adj Close", s) for s in stocks]]
df.columns = df.columns.droplevel(level=0)
df
>>
Symbols ORCL TSLA IBM YELP MSFT
Date
2014-01-02 33.703285 30.020000 137.696884 67.919998 31.983477
2014-01-03 33.613930 29.912001 138.520721 67.660004 31.768301
2014-01-06 33.479893 29.400000 138.045746 71.720001 31.096956
2014-01-07 33.819431 29.872000 140.799240 72.660004 31.337952
2014-01-08 33.703274 30.256001 139.507858 78.419998 30.778502
... ... ... ... ...
2014-12-24 41.679443 44.452000 123.015839 53.000000 42.568497
2014-12-26 41.562233 45.563999 123.411110 52.939999 42.338593
2014-12-29 41.120468 45.141998 122.019974 53.009998 41.958347
2014-12-30 40.877041 44.445999 121.670265 54.240002 41.578117
2014-12-31 40.543465 44.481998 121.966751 54.730000 41.074078