Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 64 ยท Getting Started

Series

Learn how to create a pandas Series from lists, dicts and arrays, read its index, values and dtype, do vectorized math and understand index alignment.

A Series is a one-dimensional labeled sequence of values. Think of it as one column of a spreadsheet, plus a label on every cell. Every DataFrame column is a Series, so understanding Series first makes everything else in pandas easier.

A Series has three parts you will use constantly: the values (the data), the index (a label for each value) and a dtype (the one data type shared by all values). It can also carry a name, which becomes the column header when the Series is placed in a table.

flowchart LR
    subgraph Series
      direction TB
      I["index: Mon, Tue, Wed"]
      V["values: 12, 15, 9"]
      D["dtype: int64"]
      N["name: visitors"]
    end

A Series pairs values with an index, and stores them with one dtype and an optional name.

Creating a Series

The simplest way is to pass a list. pandas adds a default index that counts from 0. You can also supply your own labels with index=, and a name=:

Python Series from a list

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line of each printed Series shows the name (if any) and the dtype. visitors.index and visitors.to_list() give you the labels and the data back as plain Python objects.

You can build a Series from other sources too:

  • A dictionary: keys become the index, values become the data.
  • A NumPy array: the data is used as it is, which is very efficient.
  • A single value with an index: the value is repeated for every label.
Python Dicts, arrays and scalars

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Note: a dictionary keeps its insertion order, so the Series index follows the order in which you wrote the keys.

Selecting values

You can read a single value by its label with square brackets, pick several labels with a list, or take a slice of positions. For clearer code in real projects, prefer .loc for labels and .iloc for positions, which later lessons (loc and iloc) explain in depth.

Python Reading values from a Series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice the slice stock.iloc[1:3]: position slices stop before the end number, just like Python lists, so you get the items at positions 1 and 2.

Vectorized arithmetic

Operations on a Series apply to every element. You do not write a loop. Operations between a Series and a single number apply that number to every element, and operations between two Series match values by index label:

Python Math on a whole Series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Comparisons such as prices > 3 return a Series of True and False. You will use these as filters in Filtering with Conditions.

Index alignment and missing values

When you combine two Series, pandas lines them up by index label, not by position. A label that exists in only one of them has nothing to pair with, so the result is missing and shown as NaN ("not a number"):

flowchart LR
    A["a: a=1, b=2, c=3"] --> R
    B["b: b=10, c=20, d=30"] --> R
    R["a + b:<br/>a=NaN, b=12, c=23, d=NaN"]

Addition matches labels. Labels that appear in only one Series produce NaN.

Python Alignment in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Two things to notice. First, integers turned into floats, because a plain integer column cannot hold NaN. Second, the method form first.add(second, fill_value=0) lets you decide what a missing partner should count as. The same method form exists for subtraction (sub), multiplication (mul) and division (div).

Warning: alignment surprises beginners most when two Series have the same length but different labels. Adding them does not pair the first with the first. If you really want to combine by position, use .to_numpy() on one side or reset the index.

Data types in a Series

Every Series has exactly one dtype. Integers get int64, decimals float64, True/False bool. Text gets str, the default string type in pandas 3. If you mix types, pandas picks the most general one that can hold everything, which is often object. Check what you got with .dtype, and convert on purpose with .astype():

Python dtype and astype

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Mixed data ends up as object, which stores arbitrary Python objects, is slower and rarely what you want in analysis. Data Types and Conversion shows how to fix that.

Useful Series methods

A few methods you will reach for every day:

  • len(s), s.size: the number of elements.
  • s.head(n) and s.tail(n): the first or last n items.
  • s.sum(), s.mean(), s.min(), s.max(): quick statistics (missing values are skipped).
  • s.sort_values() and s.sort_index(): order by values or by labels.
  • s.value_counts(): how often each distinct value occurs.
  • s.unique() and s.nunique(): distinct values and how many there are.
Python Counting and sorting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What happens when you add two Series that have different index labels?

    pandas aligns on the index. Labels without a partner produce NaN unless you use a method like add() with fill_value.
  2. 2. Which statement about a Series dtype is correct?

    A Series stores values of a single dtype. You can change it with astype(), and mixed data falls back to the general object dtype.
  3. 3. What does pd.Series({"x": 5, "y": 7}) use as the index?

    When you build a Series from a dict, the keys become the index labels and the values become the data.
  4. 4. Why does pd.Series([1, 2, 3]) + pd.Series([10, 20, 30], index=[1, 2, 3]) contain NaN?

    The first Series has labels 0, 1, 2 and the second has 1, 2, 3. Only 1 and 2 overlap, so labels 0 and 3 become NaN.

Summary

  • A Series is a labeled one-dimensional array with values, index, dtype and an optional name.
  • Build one from a list, dict, NumPy array or scalar plus an index.
  • Arithmetic and comparisons work on all elements at once, with no loop.
  • Two Series are combined by index label; labels without a partner give NaN, and add(..., fill_value=0) and friends let you choose the fill.
  • A Series has one dtype; mixed data becomes object.
  • value_counts(), unique(), sort_values() and summary methods answer most first questions.

Next: DataFrame Basics, where several Series become a table.