user2975269
user2975269

Reputation: 13

panda get_data_yahoo panel data form

Want to start loading the Yahoo data in to a panel data set utilizing the Panda's get_data_yahoo method.

So I have the below set up

all_data={}
for ticker in ['GOOG','AAPL','XOM']:
    all_data[ticker]=web.get_data_yahoo(ticker,'04/11/2014','04/14/2014')

and the output is below

{'GOOG':               Open   High     Low   Close   Volume  Adj Close
Date                                                         
2014-04-11  532.55  540.0  526.53  530.60  3914100     530.60
2014-04-14  538.25  544.1  529.56  532.52  2568000     532.52

[2 rows x 6 columns], 'AAPL':              Open    High     Low   Close   Volume  Adj Close
Date                                                         
2014-04-11  519.0  522.83  517.14  519.61  9704200     519.61
2014-04-14  521.9  522.16  517.21  521.68  7345500     521.68

[2 rows x 6 columns], 'XOM':              Open   High    Low  Close    Volume  Adj Close
Date                                                       
2014-04-11  96.51  97.44  96.28  96.72  13352500      96.72
2014-04-14  97.49  98.06  96.74  97.86   9610200      97.86

I want to add a separate column per ticker on each grab like

Date        Ticker  Open  High  Low  Close  Volume Adj Close
2014-04-11  GOOG    xxx    xxx  xxx  xxx     xxx   xxx
2014-04-14  GOOG    xxx    xxx  xxx  xxx     xxx   xxx
2014-04-11  AAPL    xxx    xxx  xxx  xxx     xxx   xxx
2014-04-14  AAPL    xxx    xxx  xxx  xxx     xxx   xxx
2014-04-11  XOM     xxx    xxx  xxx  xxx     xxx   xxx
2014-04-14  XOM     xxx    xxx  xxx  xxx     xxx   xxx

How can I achieve this?

Thanks!

Upvotes: 1

Views: 6464

Answers (1)

Karl D.
Karl D.

Reputation: 13757

You could do the following:

data=[]
for ticker in ['GOOG','AAPL','XOM']:
    x = web.get_data_yahoo(ticker,'04/11/2014','04/14/2014')
    x['ticker'] = ticker
    data.append(x)

print pd.concat(data)

             Open    High     Low   Close    Volume  Adj Close ticker
Date                                                                  
2014-04-11  532.55  540.00  526.53  530.60   3914100     530.60   GOOG
2014-04-14  538.25  544.10  529.56  532.52   2568000     532.52   GOOG
2014-04-11  519.00  522.83  517.14  519.61   9704200     519.61   AAPL
2014-04-14  521.90  522.16  517.21  521.68   7345500     521.68   AAPL
2014-04-11   96.51   97.44   96.28   96.72  13352500      96.72    XOM
2014-04-14   97.49   98.06   96.74   97.86   9610200      97.86    XOM

Upvotes: 4

Related Questions