Module 03 · Foundation

Meet the pandas Series

Create a labeled column and explain positions, labels, and arithmetic.

Before you start: Know dictionary keys, list positions, and the difference between = and ==.

Python loads from this site on your first run.

Each run starts with fresh data. Download any exported files before another run. Your written answers and code drafts are saved on this device; export your work to keep a backup.

M03C01 · Explore · predict · practice

One column, with labels

The setup imports pandas using the short name pd. A Series is a one-dimensional collection of values with an index: labels attached to those values. Without an explicit index, pandas supplies labels starting at 0. A Series also has a dtype describing how its values are stored.

Read the example

scores = pd.Series([88, 72, 91], name="Score")
print(scores)
print("Number of values:", len(scores))
print("Index:", scores.index.tolist())
Verified example output · pandas 2.2.3
0    88
1    72
2    91
Name: Score, dtype: int64
Number of values: 3
Index: [0, 1, 2]

Watch for this. Do not call the dtype text a data row; it describes the Series.

Your turn

Create a Series called hours containing 8, 5, and 9. Give it the name Study_Hours and print its length.

For the code check, keep these result variable names: hours. Different valid approaches are welcome.

Show a hint

Change both the list values and the name= argument.

Show one worked solution
hours = pd.Series([8, 5, 9], name="Study_Hours")
print(hours)
print("Number of values:", len(hours))

The Series contains three study-hour values and is named Study_Hours. Its default index is still 0, 1, 2.

Compare your explanations
  1. Read. pd is the alias for the pandas package created by import pandas as pd in the setup.
  2. Predict. 0, 1, and 2.
  3. Explain. No. It labels each score; it is not an additional measurement in the Series.

One quick check

A Series is best described as:

What you should be able to explain. I can distinguish Series values, index labels, and the Series name.

M03C02 · Explore · predict · practice

Choose labels or positions explicitly

You can supply meaningful labels, such as student IDs, as the Series index. loc selects an index label; iloc selects an integer position. These can identify the same value, but they ask different questions. Using them explicitly avoids confusing a number used as a label with a position.

Read the example

scores = pd.Series([88, 72, 91], index=["S001", "S002", "S003"], name="Score")
by_label = scores.loc["S002"]
by_position = scores.iloc[1]
print(scores)
print(by_label, by_position)
Verified example output · pandas 2.2.3
S001    88
S002    72
S003    91
Name: Score, dtype: int64
72 72

Watch for this. Prefer .iloc[0] when you mean first position; do not rely on scores[0] with a text index.

Your turn

Select S003 by label and the first score by position. Store them as by_label and by_position, then print both.

For the code check, keep these result variable names: by_label, by_position. Different valid approaches are welcome.

Show a hint

Use loc with the ID text, and iloc with 0 for the first position.

Show one worked solution
scores = pd.Series([88, 72, 91], index=["S001", "S002", "S003"], name="Score")
by_label = scores.loc["S003"]
by_position = scores.iloc[0]
print(by_label, by_position)

S003 has score 91. Position 0 contains S001's score, 88.

Compare your explanations
  1. Read. scores.loc["S002"] asks for the label S002. scores.iloc[1] asks for position 1, the second value.
  2. Predict. 72 and 72.
  3. Explain. No. Sorting can change positions. The S002 label still identifies that student's score.

One quick check

Which selector is explicitly positional?

What you should be able to explain. I can select a Series value by label or by position.

M03C03 · Explore · predict · practice

Do arithmetic on a whole Series

Pandas can apply an arithmetic operation to all values at once. This is called a vectorized operation. scores + 2 creates a new result; it does not replace scores unless you assign the result back. clip() can enforce an upper or lower boundary when that is the stated rule.

Read the example

scores = pd.Series([88, 72, 99], index=["S001", "S002", "S003"], name="Score")
result = scores + 2
print("Original:")
print(scores)
print("After adding two:")
print(result)
Verified example output · pandas 2.2.3
Original:
S001    88
S002    72
S003    99
Name: Score, dtype: int64
After adding two:
S001     90
S002     74
S003    101
Name: Score, dtype: int64

Watch for this. Use clipping only when a justified business or teaching rule calls for it.

Your turn

Add 5 to each score, then cap the result at 100. Keep the original scores unchanged.

For the code check, keep these result variable names: result, scores. Different valid approaches are welcome.

Show a hint

Apply .clip(upper=100) to the calculated Series.

Show one worked solution
scores = pd.Series([88, 72, 99], index=["S001", "S002", "S003"], name="Score")
result = (scores + 5).clip(upper=100)
print(result)

The new scores are 93, 77, and 100. Clipping applies the stated maximum; it should not be used to hide unexplained invalid source data.

Compare your explanations
  1. Read. It adds 2 to every value in the Series, keeping the index labels.
  2. Predict. The new result is 101. The original scores value remains 99.
  3. Explain. It exceeds the permitted range. If the rule allows bonus marks but caps scores at 100, clipping can apply that explicit rule.

One quick check

What does result = scores + 2 normally do?

What you should be able to explain. I can calculate a new labeled column without writing a loop.

M03C04 · Explore · predict · practice

Labels control Series alignment

When combining two Series, pandas matches index labels, even when their order differs. It does not simply pair the first value in one Series with the first value in the other. A label missing from one side normally produces a missing result for arithmetic.

Read the example

quiz = pd.Series({"S001": 17, "S002": 14, "S003": 19})
project = pd.Series({"S003": 46, "S001": 44, "S002": 36})
result = quiz + project
print(result)
Verified example output · pandas 2.2.3
S001    61
S002    50
S003    65
dtype: int64

Watch for this. Equal lengths do not guarantee correct alignment; inspect the actual index labels.

Your turn

Remove S002 from project, add the two Series again, and print which result is missing. Do not replace the missing project with zero.

For the code check, keep these result variable names: result. Different valid approaches are welcome.

Show a hint

Build project with only S003 and S001; use result.isna() to inspect missing results.

Show one worked solution
quiz = pd.Series({"S001": 17, "S002": 14, "S003": 19})
project = pd.Series({"S003": 46, "S001": 44})
result = quiz + project
print(result)
print("Missing total:")
print(result.isna())

S001 totals 61 and S003 totals 65. S002 has a missing total because its project value is absent. A missing project is not automatically a zero score.

Compare your explanations
  1. Read. 44, because its label is S001, even though it is the second value in project.
  2. Predict. 65, from 19 + 46.
  3. Explain. It keeps values matched to the same student when the two inputs have different row orders, provided their labels identify students correctly.

One quick check

How does pandas add two Series with index labels?

What you should be able to explain. I can explain why label alignment can create missing values.

Further detail: official documentation for this topic.