Series & DataFrames
15 min
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
Series operations return another Series (or a scalar for aggregates like .sum()) — the labels travel with the data through almost every operation.
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
Write `make_series(values, labels)`, returning a `pd.Series` built from `values` with `labels` as its index.
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.
What's the key difference between a pandas Series and a DataFrame?
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.