Lesson 59 · Mastering Pandas
Volatility and Risk Analysis Training for Finance and Stock Market
Welcome! In this lesson, we will go beyond basic pandas and use real stock data. Explore, clean, and transform time series. Analyze performance and trends…
- CourseMastering Pandas
- Lesson59 of 44
- Video21 min
- FormatJupyter notebook · 18 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynbIntermediate Pandas: Analysing Financial and Stock Market Data#
Welcome! In this lesson, we will go beyond basic pandas and use real stock data.
- Explore, clean, and transform time series.
- Analyze performance and trends with grouping and pivoting.
- Visualize results for actionable insights.
We will use Apple Inc.'s daily historical stock prices.
Let us dive in!
import warnings
warnings.filterwarnings("ignore")
# Import libraries for data and visualization
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(42)
# Data setup (Stock Market Data via Yahoo Finance)
df = yf.download('AAPL', start='2022-01-01', end='2023-12-31')
print(df.shape)
print(df.head(3))
Exploring Stock Data: Columns and Index#
When you work with financial data, it is key to understand your columns and the index.
- The index here is the date.
- Columns: Open, High, Low, Close, Adj Close, and Volume.
Let us learn how to inspect their names and types.
print(df.columns)
print(df.index)
print(df.info())
# Check for missing values
print(df.isnull().sum())
# Fill missing data with forward fill
df_filled = df.fillna(method='ffill')
print(df_filled.isnull().sum())
Calculating Daily Returns#
In finance, we often want to know how much a stock changed each day.
This helps us spot periods of big losses or gains.
Let us compute daily percentage returns.
# Add daily returns column
df_filled['Daily Return'] = df_filled['Adj Close'].pct_change() * 100
print(df_filled[['Adj Close', 'Daily Return']].head())
# Describe the returns
print(df_filled['Daily Return'].describe())
Visualizing the Time Series#
Charts make it simple to spot trends and volatility in financial markets.
Let us plot the closing price and daily returns.
df_filled['Adj Close'].plot(figsize=(12, 5), title='AAPL Adjusted Close (2022-2023)')
plt.ylabel('Price in USD')
plt.show()
df_filled['Daily Return'].plot(kind='hist', bins=30, title='Histogram of Daily Returns', figsize=(8,4))
plt.xlabel('Daily Return (%)')
plt.show()
# Calculate rolling 20-day mean and plot
df_filled['20D_MA'] = df_filled['Adj Close'].rolling(window=20).mean()
df_filled['Adj Close'].plot(figsize=(12,5), label='Adj Close')
df_filled['20D_MA'].plot(label='20-Day Moving Avg')
plt.legend()
plt.title('AAPL: Price and 20-Day Moving Average')
plt.show()
Group and Resample: Monthly Analysis#
Raw daily data can be overwhelming. Resampling lets us zoom out and see the bigger monthly picture.
Let us check average adjusted close price for each month.
monthly_mean = df_filled['Adj Close'].resample('M').mean()
print(monthly_mean.head())
# Visualize monthly average price
monthly_mean.plot(marker='o', figsize=(10,4), title='AAPL Monthly Avg Close')
plt.ylabel('Price in USD')
plt.show()
# Find the month with highest and lowest average price
best_month = monthly_mean.idxmax()
worst_month = monthly_mean.idxmin()
print(f'Highest average price: {monthly_mean.max():.2f} in {best_month.strftime("%B %Y")}' )
print(f'Lowest average price: {monthly_mean.min():.2f} in {worst_month.strftime("%B %Y")}' )
Pivot Tables: Group by Weekday#
Do certain days of the week have higher returns than others?
Let us use a pivot table to compare daily returns by weekday.
df_filled['Weekday'] = df_filled.index.day_name()
weekday_returns = df_filled.pivot_table(values='Daily Return', index='Weekday', aggfunc='mean')
print(weekday_returns)
Mini-Project: Volatility and Risk#
Volatility means how much a stock price changes day to day. It is a key risk measure in finance.
Let us calculate and plot the rolling 20-day standard deviation of daily returns.
This is a simple way to see when the stock was safest or riskiest.
df_filled['20D_Volatility'] = df_filled['Daily Return'].rolling(window=20).std()
df_filled['20D_Volatility'].plot(figsize=(12,5), title='AAPL 20-Day Rolling Volatility')
plt.ylabel('Volatility (%)')
plt.show()
# Challenge: What if you want to compare with Nasdaq or Microsoft?
tick = input('Enter another stock ticker, like MSFT or ^IXIC: ')
df2 = yf.download(tick, start='2022-01-01', end='2023-12-31')
print(df2.head(3))
Best Practices & Troubleshooting#
- Always check for missing values and data types up front.
- Visualize early and often.
- Document your workflow step by step.
- If columns do not line up after merging, check for index mismatches.
Tip: If you hit import errors, try installing with:
!pip install yfinance matplotlib
# CHALLENGE: Try these!
# 1. Calculate annual return for 2023 vs 2022.
# 2. Plot closing prices for Apple and your other stock together.
Recap: What Did We Learn?#
- How to load and inspect real stock market data
- How to fill missing values and calculate returns
- How to group, resample, and visualize time series
- How to analyze risk with rolling volatility
- How to compare multiple stocks
Keep practicing with new tickers and time windows.
Thanks for Learning! Next Steps#
- Try bringing in other companies or sectors for deeper comparisons.
- Adapt your workflow for any time series: weather, sales, or even social media metrics.
Like, Subscribe, and Share if this helped you, and check out our channel for more Pandas and finance tutorials!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



