Latticework

Command Palette

Search for a command to run...

Pandas

Series & DataFrames

15 min

Explanation

pandas builds on NumPy to add labels: a Series is a 1D array where every value has an associated index label, instead of just a position.

import pandas as pd

prices = pd.Series([100, 105, 98], index=["mon", "tue", "wed"])
print(prices["tue"])   # 105 — look up by label, not just position
print(prices.mean())    # 101.0
Try it

Series operations return another Series (or a scalar for aggregates like .sum()) — the labels travel with the data through almost every operation.

Loading editor…
Explanation

A DataFrame is a table: multiple columns (each really a Series), sharing one row index. It's the structure you'll spend the most time with in pandas — think of it as a spreadsheet you can query and transform with code.

df = pd.DataFrame({
    "name": ["Ada", "Grace", "Alan"],
    "score": [92, 88, 95],
})
print(df.shape)     # (3, 2) -- 3 rows, 2 columns
print(df["score"])   # the 'score' column, as a Series
print(df["score"].sum())  # 275
Exercise

Write `make_series(values, labels)`, returning a `pd.Series` built from `values` with `labels` as its index.

Exercise

Write `column_total(data, col)`: build a `pd.DataFrame` from the dict `data` (column name → list of values) and return the sum of column `col` as a plain Python int.

Quiz

What's the key difference between a pandas Series and a DataFrame?

Checkpoint

You can build a Series with a custom index and a DataFrame from a dict of columns, and pull a single column out as a Series.