1

I'm quite new to pandas (and coding in general), but am really enjoying messing around with pulling stock data from Yahoo Finance.

I was just wondering if there's a way to also pull the name of the exchange that the stock is listed on (i.e. LSE, NYSE, AIM etc), as well as the currency the stock is listed in from Yahoo?

This is my code so far (I'll work on adding some axis labels when I'm back from work tonight):

import pandas as pd
import sys
import matplotlib
import matplotlib.pyplot as plt
import pandas_datareader.data as web
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
print('Matplotlib version ' + matplotlib.__version__)

symbols_list = ['ORCL', 'AAPL', 'TSLA']
d = {}
for x in symbols_list:
    d[x] = web.DataReader(x, "yahoo", '2012-12-01')
ticker = pd.Panel(d)
df1 = ticker.minor_xs('Adj Close')
print df1

fig = plt.figure()
fig.suptitle("Stock Prices", fontsize=36, fontweight='bold')

plt.plot(df1)
plt.legend(ticker, loc='best', shadow=True, fontsize=36)
plt.show()
Jason Johnston
  • 17,194
  • 2
  • 20
  • 34
Andy
  • 27
  • 1
  • 1
  • 5

1 Answers1

0

I think you can read_csv from link, filter columns and then concat them to df. Then you can use loc for maping:

import pandas as pd
import sys
import matplotlib
import matplotlib.pyplot as plt
import pandas_datareader.data as web
print('Python version ' + sys.version)
print('Pandas version ' + pd.__version__)
print('Matplotlib version ' + matplotlib.__version__)



df_NASDAQ = pd.read_csv('http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download',
                        usecols=['Symbol', 'Name'])
#print (df_NASDAQ.head())

df_NYSE = pd.read_csv('http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download',
                        usecols=['Symbol', 'Name'])
#print (df_NYSE.head())

df_AMEX = pd.read_csv('http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download',
                        usecols=['Symbol', 'Name'])
#print (df_AMEX.head())

df = pd.concat([df_NASDAQ, df_NYSE, df_AMEX]).set_index('Symbol')
print (df.head())
                                          Name
Symbol                                        
TFSC                        1347 Capital Corp.
TFSCR                       1347 Capital Corp.
TFSCU                       1347 Capital Corp.
TFSCW                       1347 Capital Corp.
PIH     1347 Property Insurance Holdings, Inc.
symbols_list = ['ORCL', 'AAPL', 'TSLA']
d = {}
for x in symbols_list:
    print (x, df.loc[x, 'Name'])
ORCL Oracle Corporation
AAPL Apple Inc.
TSLA Tesla Motors, Inc.

    #d[ x ] = web.DataReader(x, "yahoo", '2012-12-01')
    d[ df.loc[x, 'Name'] ] = web.DataReader(x, "yahoo", '2012-12-01')
ticker = pd.Panel(d)
df1 = ticker.minor_xs('Adj Close')
print (df1.head())

fig = plt.figure()
fig.suptitle("Stock Prices", fontsize=36, fontweight='bold')

plt.plot(df1)
plt.legend(ticker, loc='best', shadow=True, fontsize=36)
plt.show()
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • I try check currency too, but it seems always is in dollars. – jezrael Jun 01 '16 at 12:18
  • And now I try read your question again and I am not sure if I understand it correctly. So please check answer and if something wrong, I try repait it. – jezrael Jun 01 '16 at 12:19
  • Ah OK, I'm based in the UK it'd be really useful if I could find a way to include the currency somewhere. I'll keep looking! I'll check your solution tonight for getting the exchange listed somewhere - it looks like it will only show those listed on the NYSE/NASDAQ/AMEX, but I'm sure I can edit the code you wrote to pull LSE/AIM related data too. Thanks for your reply! – Andy Jun 01 '16 at 12:28
  • Thank you - and link for answer was found [here](http://stackoverflow.com/questions/5015016/yahoo-finance-api). – jezrael Jun 01 '16 at 12:30