Search code examples
pandascurrencyyahooyahoo-finance

Currency and Exchange Name from Yahoo


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()

Solution

  • 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()