Lesson 9 · Finance and Stock Market Analytics
Introduction to Pandas for Stock Data Analysis
Pandas is a powerful Python package for working with tabular data. Stock market data is naturally tabular and time-based, making it a great fit for Pandas.…
- CourseFinance and Stock Market Analytics
- Lesson9 of 16
- Video20 min
- FormatJupyter notebook · 17 code cells
What you'll learn
- What kind of stock data will we explore?
- Core columns in our stock market dataset
- Intermediate: Pivot data to wide format for price comparison
- Intermediate: Filter for high price swings
- Advanced: Compute a rolling 20-day average closing price for Tesla
- Error handling: What if a ticker has missing data?
- Common patterns in stock data analysis
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbIntroduction to Pandas for Stock Data#
- Pandas is a powerful Python package for working with tabular data.
- Stock market data is naturally tabular and time-based, making it a great fit for Pandas.
- In this lesson, you will learn how to load, inspect, and analyze real stock market data using Pandas.
- We will use real world examples from large tech companies.
- By the end, you will be able to answer questions and spot patterns in stocks with confidence.
import warnings
warnings.filterwarnings('ignore')
import pandas as pd
import yfinance as yf
What kind of stock data will we explore?#
- We will work with real daily Open, High, Low, Close, and Volume (OHLCV) data.
- Each day and ticker has its own row.
- The data is in 'long format', with columns like Date, Ticker, Close, High, Low, Open, Volume.
- Beginners often get confused by wide vs long format data.
- MultiIndex data from yfinance can trick new users. We will work with a clean flattened table instead.
# Load real multi-ticker OHLCV stock data in long format
tickers = ['AAPL','MSFT','GOOGL','AMZN','TSLA']
ohlcv = yf.download(tickers, period='1y', auto_adjust=True, progress=False)
ohlcv = ohlcv.stack(future_stack=True).rename_axis(['Date','Ticker']).reset_index()
ohlcv.columns.name = None
print(ohlcv.shape)
print(ohlcv.head(3))
Core columns in our stock market dataset#
- Date: The trading date (YYYY-MM-DD).
- Ticker: The stock ticker symbol (for example, AAPL for Apple).
- Open: The price at which the stock started trading that day.
- High: The highest price that day.
- Low: The lowest price that day.
- Close: The price at the market close.
- Volume: The number of shares traded that day.
- Pay close attention to column meanings! They are vital for finance analysis.
# Example: View all available tickers in our OHLCV data
print(ohlcv['Ticker'].unique())
# Example: Select all rows for Apple (AAPL)
apple = ohlcv[ohlcv['Ticker'] == 'AAPL']
print(apple.head(2))
# Example: What is the date range in our dataset?
print('First date:', ohlcv['Date'].min())
print('Last date:', ohlcv['Date'].max())
# Example: Count the number of trading days for each ticker
counts = ohlcv.groupby('Ticker').size()
print(counts)
Intermediate: Pivot data to wide format for price comparison#
- In real finance, you may want to compare closing prices across stocks on the same date.
- Pandas 'pivot' allows you to reshape from long to wide format.
- Rows become dates, columns become tickers, and values are selected columns (for example, Close).
- Be careful: beginners sometimes confuse index and columns in pivots.
# Pivot: Create a wide table of close prices (dates vs tickers)
wide = ohlcv.pivot(index='Date', columns='Ticker', values='Close')
print(wide.head(2))
# Example: Compute daily price change in percent for Apple
apple['Pct_Change'] = apple['Close'].pct_change() * 100
print(apple[['Date','Close','Pct_Change']].head(5))
Intermediate: Filter for high price swings#
- In risk management, traders look for days with unusually large price moves.
- You can filter rows where percent change (up or down) exceeds 3 percent.
- This is often used in volatility screening and backtesting.
# Filter Apple rows for large positive or negative price moves
swings = apple[apple['Pct_Change'].abs() > 3]
print(swings[['Date','Close','Pct_Change']])
Advanced: Compute a rolling 20-day average closing price for Tesla#
- Rolling means a moving window, often used by traders and analysts.
- The average smooths out day-to-day noise, showing the trend.
- Rolling analysis is used in technical indicators like moving averages.
- Beginners often forget to .sort_values('Date') before rolling if data is unordered.
# Calculate a 20-day rolling mean for TSLA closing prices
tesla = ohlcv[ohlcv['Ticker'] == 'TSLA'].sort_values('Date')
tesla['SMA20'] = tesla['Close'].rolling(window=20).mean()
print(tesla[['Date','Close','SMA20']].tail(7))
# Advanced: Find the highest volume day for Google
google = ohlcv[ohlcv['Ticker'] == 'GOOGL']
max_vol = google.loc[google['Volume'].idxmax()]
print('Highest volume day for GOOGL:', max_vol['Date'])
print('Volume:', int(max_vol['Volume']))
# Advanced: Find all days where Amazon closed higher than both Apple and Tesla
pivot_close = ohlcv.pivot(index='Date', columns='Ticker', values='Close')
mask = (pivot_close['AMZN'] > pivot_close['AAPL']) & (pivot_close['AMZN'] > pivot_close['TSLA'])
result = pivot_close[mask]
print(result[['AMZN','AAPL','TSLA']].head())
Error handling: What if a ticker has missing data?#
- In real markets, holidays and IPOs can cause missing trading days for some stocks.
- Always check for missing values with pd.isna or .isnull.
- You can fill or drop missing data depending on your business need.
# Detect missing (NA) values in the long OHLCV table
print(ohlcv.isnull().sum())
# Best practice: Always check your Date column type
print('Date column dtype:', ohlcv['Date'].dtype)
ohlcv['Date'] = pd.to_datetime(ohlcv['Date'])
print('After conversion:', ohlcv['Date'].dtype)
Common patterns in stock data analysis#
- Filtering on ticker or date.
- Calculating percent returns.
- Creating moving averages.
- Handling missing data wisely.
- Pivoting to wide format for comparison.
- Always document your steps as comments.
# Practice: Select Tesla closing prices during the last three months
last_3mo = tesla[tesla['Date'] >= (tesla['Date'].max() - pd.Timedelta(days=90))]
print(last_3mo[['Date','Close']].head())
# End-to-end example: Identify top 2 up days for each ticker
top2 = ohlcv.copy()
top2['Pct_Change'] = top2.groupby('Ticker')['Close'].pct_change() * 100
ranks = top2.groupby('Ticker')['Pct_Change'].nlargest(2).reset_index()
result = top2.loc[ranks['level_1'], ['Date','Ticker','Close','Pct_Change']]
print(result.sort_values(['Ticker','Pct_Change'], ascending=[True, False]))
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



