Series Math and Alignment
How labels auto-align during operations.
Series Math and Alignment is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Math on Whole Columns
You can do math on an entire Series at once. Operations apply to every value together, so there is no loop to write. ⚡
Add a Scalar
Adding a single number to a Series adds it to every value. This broadcast happens automatically across the whole column.
import pandas as pd
s = pd.Series([10, 20, 30])
print(s + 5)All the Operators Work
Subtraction, multiplication, and division behave the same way. Each one is vectorized, touching all values in a single fast pass.
print(s * 2)Add Two Series
When you add two Series, pandas lines them up by their index labels first. This step is called alignment and it is the headline feature. 🔗
Matching Labels Combine
Where both Series share a label, their values are combined. Alignment means position does not matter, only the labels do.
a = pd.Series([1, 2], index=['x', 'y'])
b = pd.Series([10, 20], index=['x', 'y'])
print(a + b)Order Does Not Matter
Even if the labels appear in a different order, pandas still matches them correctly. It pairs label to label, never row to row.
b = pd.Series([20, 10], index=['y', 'x'])
print(a + b)Missing Labels Make NaN
If a label exists in only one Series, the result for it becomes NaN, meaning Not a Number. There was nothing to pair it with.
c = pd.Series([1], index=['x'])
print(a + c)Fill the Gaps
Use the .add method with fill_value to treat missing labels as a default instead of NaN. Here zero plugs the holes.
print(a.add(c, fill_value=0))Apply a Function
Many NumPy functions work directly on a Series and return a new one. Each value is transformed while its index stays intact.
import numpy as np
print(np.sqrt(pd.Series([4, 9, 16])))Compare to Make Masks
Comparisons also run element-wise, giving a Series of True and False. That boolean mask is the start of filtering data.
print(s > 15)Why Alignment Wins
Automatic alignment quietly prevents a whole class of bugs from mismatched rows. Trust the labels and let pandas pair them for you. 🛡️
Quick Check
Let's confirm how alignment handles a label gap.
Recap
You saw Series math is vectorized, that operations align by index label, and that unmatched labels yield NaN unless you fill them. Great progress! 🎉
Frequently asked questions
Is the “Series Math and Alignment” lesson free?
Yes — the full text of “Series Math and Alignment” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Series Math and Alignment”?
How labels auto-align during operations. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Series Math and Alignment” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- A Column With a Name and Index
- Label-Based vs Position-Based Access
- Series Math and Alignment
- Counting and Spotting Values