Select stocks and get data from Yahoo! Finance

Importing and Managing Financial Data in Python

Stefan Jansen

Instructor

Select stocks based on criteria

  • Use the listing information to select specific stocks
  • As criteria:
    • Stock Exchange
    • Sector or Industry
    • IPO Year
    • Market Capitalization
Importing and Managing Financial Data in Python

Get ticker for largest company

nyse = pd.read_excel('listings.xlsx',sheet_name='nyse', na_values='n/a')
nyse = nyse.sort_values('Market Capitalization', ascending=False)

nyse[['Stock Symbol', 'Company Name']].head(3)
     Stock Symbol             Company Name
1586          JNJ        Johnson & Johnson
1125          XOM  Exxon Mobil Corporation
1548          JPM    J P Morgan Chase & Co
largest_by_market_cap = nyse.iloc[0]  # 1st row

largest_by_market_cap['Stock Symbol'] # Select row label
'JNJ'
Importing and Managing Financial Data in Python

Get ticker for largest company

nyse = nyse.set_index('Stock Symbol') # Stock ticker as index
nyse.info()
Index: 3147 entries, JNJ to EAE
Data columns (total 6 columns):
 #   Column                 Non-Null Count  Dtype  
 --  ------                 --------------  -----  
 0   Company Name           3147 non-null   object 
 1   Last Sale              3079 non-null   float64
 2   Market Capitalization  3147 non-null   float64
...
nyse['Market Capitalization'].idxmax() # Index of max value
'JNJ'
Importing and Managing Financial Data in Python

Get ticker for largest tech company

nyse['Sector'].unique() # Unique values as numpy array
array(['Technology', 'Health Care', ...], dtype=object)
tech = nyse.loc[nyse.Sector == 'Technology']
tech['Company Name'].head(2)
Stock Symbol                        Company Name            
ORCL                          Oracle Corporation
TSM           Taiwan Semiconductor Manufacturing
nyse.loc[nyse.Sector=='Technology', 'Market Capitalization'].idxmax()
'ORCL'
Importing and Managing Financial Data in Python

Get data for largest tech company with 2017 IPO

ticker = nyse.loc[(nyse.Sector=='Technology') & (nyse['IPO Year']==2017), 
                 'Market Capitalization'].idxmax()

data = DataReader(ticker, 'yahoo') # Start: 2010/1/1 data = data.loc[:, ['Close', 'Volume']]
Importing and Managing Financial Data in Python

Visualize price and volume on two axes

import matplotlib.pyplot as plt
data.plot(title=ticker, secondary_y='Volume')
plt.tight_layout(); plt.show()

ugly_plot.png

Importing and Managing Financial Data in Python

Let's practice!

Importing and Managing Financial Data in Python

Preparing Video For Download...