Series: one labelled column

Intermediate

Introduction

A Series is the building block of pandas: one column of values with a label for each value. Labels make data easier to read than positions (prices["milk"] rather than prices[2]), and they let pandas line values up correctly when two Series are combined. That lining up, alignment, is the behaviour that surprises newcomers most, so this lesson builds it step by step.

How it works

A Series is created from a list of values and, optionally, an index of labels. Without labels, pandas numbers the rows 0, 1, 2 and so on.

import pandas as pd

s = pd.Series([3, 2, 1], index=["apple", "bread", "milk"])
s["bread"]          # 2: by label
s.loc["bread"]      # 2: by label, explicitly
s.iloc[0]           # 3: by position
(s * 2).tolist()    # [6, 4, 2]
(s > 1).tolist()    # [True, True, False]

There are two ways to select. Label-based selection uses s["bread"] or s.loc["bread"]; position-based selection uses s.iloc[0]. Prefer loc and iloc in real code: they say clearly which one you mean, which matters when the labels are themselves numbers.

Arithmetic and comparisons work on the whole Series at once: s * 2 doubles every value, s > 1 gives a Series of booleans. When two Series are combined, pandas first aligns them by label. Values with the same label are added together, whatever their positions. A label that appears in only one of them has nothing to pair with, so the result there is NaN, pandas' marker for a missing number:

extra = pd.Series([1, 2, 4], index=["bread", "milk", "tea"])
(s + extra).isna().tolist()              # apple and tea are missing: NaN
s.add(extra, fill_value=0)["tea"]        # 4.0: a missing side counts as 0

The result's index is the union of both sides' labels. Because NaN is a floating-point value, a result containing NaN is stored as floats, even if all the inputs were whole numbers. When a missing side should count as zero, use the method form s.add(extra, fill_value=0) instead of +.

In our model, a Series is a dictionary from label to value, and None stands for NaN. Alignment is a loop over the union of the labels.

Example

# A Series: values with an index of labels. Arithmetic lines up by label, not by position.
prices = {"apple": 3, "bread": 2, "milk": 1}      # like pd.Series([3, 2, 1], index=[...])
extra = {"bread": 1, "milk": 2, "tea": 4}

print("prices['bread'] =", prices["bread"])                # like s["bread"]
print("position 0 =", list(prices.values())[0])            # like s.iloc[0]
doubled = {k: v * 2 for k, v in prices.items()}           # like s * 2
print("doubled:", doubled)

# s1 + s2: every label from either side; a label missing on one side gives NaN
total = {}
for label in sorted(set(prices) | set(extra)):
    if label in prices and label in extra:
        total[label] = prices[label] + extra[label]
    else:
        total[label] = None                                  # NaN
print("sum:", total)

# s1.add(s2, fill_value=0): a missing side counts as 0
filled = {label: prices.get(label, 0) + extra.get(label, 0) for label in sorted(set(prices) | set(extra))}
print("filled:", filled)
Open in compiler

Line by line

  1. prices and extra are two Series, modelled as dictionaries from label to value.
  2. Selecting by label and by position gives bread's value and the first value.
  3. Doubling is applied to every value, like s * 2.
  4. The sum walks the union of the labels: bread and milk are on both sides, apple and tea on one only, so they become NaN (None).
  5. With a fill value of 0, every label gets a number.

Expected result

prices['bread'] = 2
position 0 = 3
doubled: {'apple': 6, 'bread': 4, 'milk': 2}
sum: {'apple': None, 'bread': 3, 'milk': 3, 'tea': None}
filled: {'apple': 3, 'bread': 3, 'milk': 3, 'tea': 4}

In the real world

Alignment is what makes pandas safe for real data: monthly sales from two shops can be added even if one shop is closed in August, and the totals land on the right months. It is also behind many surprising NaNs in beginners' work, usually from adding two Series whose labels differ by a typo, a different case, or an index that was reset on one side only.

Common mistakes

  • Expecting values to pair up by position when adding two Series; they pair by label.
  • Using s[0] on a Series whose labels are numbers, where it means the label 0, not the first position.
  • Being surprised by floats after an operation that introduced NaN.
  • Comparing with == to NaN: NaN is not equal to anything, including itself; use isna().

Best practices

  • Select with loc for labels and iloc for positions.
  • Check the index before combining two Series, and use fill_value when a missing side means zero.
  • Give Series meaningful labels, such as dates, codes or names.

Summary

A Series is a column of values with an index of labels. loc selects by label and iloc by position, and arithmetic applies to the whole Series. When two Series are combined, pandas aligns them by label; labels missing on one side give NaN, unless a fill value is supplied, and NaN turns whole numbers into floats.

Quick check

1. What does s.iloc[0] return?
2. s has labels a and b; t has labels b and c. What is (s + t) at label a?
3. Why can adding two integer Series give floats?
4. How do you make a missing side count as zero?

Key takeaways

  • A Series is values plus labels; loc selects by label and iloc by position.
  • Arithmetic works on the whole Series and lines values up by label.
  • A label present on only one side gives NaN, unless fill_value supplies a default.

Practice problems for this lesson

My notes

Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.