0
votes

Hi guys i would like to download multiple stocks from yahoo finance using Pandas.
But at the same time I need to save only the "Adj Close" column for each stock.
Moreover I would like to create a DataFrame with all this "Adj Close" columns and set the columns name as the stock ticker.

I tried to use this code but I'm stuck.

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

Hope anyone can help me

1

1 Answers

1
votes
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