Basic

Working with tables in pandas

pandas is the Python tool nearly everyone in finance reaches for when dealing with tables of data. A quant uses it to load prices, do math across whole rows, and look for patterns in seconds rather than hours.

About 2 min of reading

Data Flow

Prices load into a DataFrame, then a calculation runs against whole columns at once.
1 Load prices into a DataFrame
↓
2 Select a column
↓
3 Apply a calculation
↓
4 Read the result
Why this works
Rows and columns

A pandas table is called a DataFrame. Rows are usually days, and columns are things like open, high, low, and close. You grab a column by its name, and everything else happens on top of that.

Speed over spreadsheets

pandas does in seconds what takes minutes in Excel, and keeps working when the data gets too big for the spreadsheet to even open. A quant can pull years of prices and summarize the whole thing in a few lines.

Examples

A table that holds your prices

import pandas as pd

prices = pd.DataFrame({
    'date': ['2024-01-01', '2024-01-02'],
    'close': [150, 153]
})
print(prices['close'].mean())   # average price

That same line of code does the exact thing whether you hand it two prices or two million. pandas was built for the moment a spreadsheet stops keeping up.

Adding a new column from an old one

import pandas as pd
df = pd.read_csv('spy.csv')
df['return'] = df['close'].pct_change()
print(df['return'].mean())

Turning prices into returns, the percentage change each day, is one of the most common first steps in this field. It happens in a single line, which is what lets the analysis run fast enough to actually be useful.

Collapsing a whole column into one number

print(df['return'].std())   # volatility
print(df['return'].max())   # best day

A whole year of data, compressed into a single volatility number or a single best day. That's how a quant measures risk without squinting at a spreadsheet of thousands of cells.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The math behind a market bet
Previous Lesson
The math behind a market bet
Next Lesson
The three numbers that actually mean something
The three numbers that actually mean something →