LambdaLab
Informatics Practices · Class 12 · Python Pandas · Series
PandasSeries⏱️ 9 min read

Vectorized Maths

Just like a NumPy array, a Series lets you do maths on every value at once — no loops. There are three flavours: maths with a single number, maths between two Series, and asking the Series true/false questions.

1Scalar operations (Series & a single number)

When you combine a Series with one number (a scalar), the operation is applied to every item automatically. The index stays exactly as it was.

scalar_ops.py
Tip
Try changing + 5 to ** 2 (power) or - 10 (subtraction) and re-run. Every element updates in one shot.

2Vector operations — Pandas aligns by label

When you combine two Series, Pandas doesn't just line them up top-to-bottom. It matches values by their index label. Where a label exists in both, the maths happens. Where a label is missing on either side, the result is NaN. Explore it:

How Pandas aligns two Series

Pandas matches by LABEL, not by position. A label missing on either side becomes NaN. Change the operator.

scores
Aarav10
Priya20
Rohan30
Sneha40
other_scores
Aarav5
Priya12
Vivaan20
result
Aarav15.0
Priya32.0
RohanNaN
SnehaNaN
VivaanNaN
label in both → maths happenslabel in only one → NaNA NaN makes the whole result float64 (note the .0).

First, two Series with the same labels — everything matches:

vector_matching.py

Now watch what happens when the labels don't line up — Rohan and Sneha are missing from the second Series, and Vivaan is new:

vector_nonmatching.py
Key Takeaway
Pandas aligns two Series by label, not position. A label present in only one of them produces NaN in the result.

3Boolean filtering — ask a true/false question

A comparison (>, <, ==, !=, …) turns a Series into a Series of True/False. Feed that back inside square brackets and Pandas keeps only the rows that were True — all in one line.

boolean_filter.py
Tip
Read scores[scores > 25] as "from scores, give me the items where the score is more than 25". Change 25 to 15 and re-run to see more rows survive.
Quick Check

scoresA + scoresB, where label 'Rohan' is only in scoresA. What is the result for 'Rohan'?

Quick Check

What does scores[scores > 25] return?