Sub topic 1
Columns are Series: the building block
Prerequisites
This chapter assumes you know basic Python syntax, including functions and lambdas. It also assumes you can create a pandas DataFrame and select a column by name. NumPy knowledge helps but is not required.
A column is a Series
A DataFrame is a set of columns that share one row index. Each column is a pandas.Series, which is a one-dimensional labeled array with a single data type (dtype). When you write df["price"], you get that Series back.
import pandas as pd
df = pd.DataFrame({
"item": ["pen", "book", "lamp"],
"price": [1.5, 12.0, 30.0],
"qty": [10, 3, 2],
})
col = df["price"]
print(type(col))
print(col.dtype)
print(col.index)The output shows pandas.core.series.Series, the dtype float64, and a RangeIndex from 0 to 2. The Series carries the DataFrame's index with it. That shared index is what makes column arithmetic work.
Why this matters for new columns
Every new column you build is also a Series. If you can produce a Series with the right index, you can store it as a column. Most of this tutorial is about producing that Series in different ways: arithmetic, conditions, text cleaning, date parts, lookups, and group statistics.
print(df["price"] * df["qty"])This multiplies the two columns row by row and returns a new Series of 15.0, 36.0 and 60.0. Nothing is stored in df yet. The result is a standalone Series until you assign it to a column name.
Alignment by index
pandas matches values by index label, not by position. When two Series have the same index, the operation pairs rows with the same label. When the labels differ, pandas aligns them first, and labels that appear in only one Series produce NaN.
a = pd.Series([1, 2, 3], index=[0, 1, 2])
b = pd.Series([10, 20, 30], index=[1, 2, 3])
print(a + b)The result has labels 0 to 3. Labels 1 and 2 are summed (12.0 and 23.0). Labels 0 and 3 are NaN because each exists in only one input. The dtype becomes float64 because NaN is a float.
Common errors
- Misaligned indexes. After filtering or sorting, a Series may keep its old labels. Combining it with a column that has different labels silently inserts
NaN. Check.indexwhen a new column shows unexpected missing values. - Assuming positional matching. Assigning a Series to a column aligns on labels. Assigning a plain list or NumPy array matches by position and must have the same length as the DataFrame, or pandas raises a
ValueError. - Treating a Series like a list. A Series supports
+and*element by element. A Python list does not:[1, 2] * 2repeats the list.
df["code"] = pd.Series([100, 200, 300], index=[2, 1, 0])
print(df[["item", "code"]])Because assignment aligns on labels, the row labeled 0 gets 300, the row labeled 1 gets 200, and the row labeled 2 gets 100. The order of the values in the Series does not matter.
Exercise
Given s1 = pd.Series([1, 2], index=["a", "b"]) and s2 = pd.Series([5, 6], index=["b", "c"]), what does s1 + s2 return?
Show answer
A Series with labels a, b and c and values NaN, 7.0 and NaN. Only label b exists in both, so only it is summed (2 + 5). The others become NaN.